Thank you, that really made things much easier and admittedly much less nasty too.
Regards Rolf On 01/03/12 18:14, Peter Otten wrote: > Rolf Wester wrote: > >> The reason to use exec is just laziness. I have quite a lot of classes >> representing material data and every class has a number of parameters. >> The parameter are Magnitude objects (they have a value and a unit and >> overloaded special functions to correctly handle the units). I want to >> have getter functions that either return the Magnitude object or just the >> value: >> >> iron = Iron() >> iron.rho(0) => value >> iron.rho() => Magnitude object >> >> def rho(self, uf=1): >> if uf == 1: >> return self._rho >> else: >> return self._rho.val >> >> And because this would mean quite a lot of writing I tried to do it with >> exec. > > Make the Magnitude class callable then: > >>>> class Magnitude(object): > ... def __init__(self, value): > ... self.value = value > ... def __call__(self, uf=1): > ... if uf == 1: > ... return self > ... return self.value > ... >>>> class Iron(object): > ... def __init__(self): > ... self.rho = Magnitude(42) > ... >>>> iron = Iron() >>>> iron.rho() > <__main__.Magnitude object at 0x7fb94062be10> >>>> iron.rho(0) > 42 > > -- http://mail.python.org/mailman/listinfo/python-list