property data-descriptor - how to pass additional constants to the get/set method

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 26 02:12:49 EST 2008


En Mon, 25 Feb 2008 18:50:20 -0200, <petr.jakes.tpc at gmail.com> escribió:

> Hi,
> I would like to pass additional constants to the property data-
> descriptor, so it is not necessary to write many methods which differ
> just by constant defined in the method body.
> Till now I can do this:
>
> class MyClass(object):
>
>     def _getValue(self, myConstant):
>         print 'myConstant:', myConstant
>         return myConstant+10
>
>     def _setValue(self, inputValue, myConstant):
>         print 'inputValue:', inputValue, 'myConstant:', myConstant
>         return inputValue*myConstant
>
>     fistAttribte = property(fget = lambda self:
> self._getValue(myConstant=0),
>                        fset = lambda self, inputValue, myConstant=0:
> self._setValue(inputValue, myConstant))
>
>     secAttribute = property(fget = lambda self:
> self._getValue(myConstant=1),
>                        fset = lambda self, inputValue, myConstant=1:
> self._setValue(inputValue, myConstant))
>
> It works, but it does not look very nice to me.
> Is there some different/nicer/more pythonic way how to achieve above
> mentioned?

Try using functools.partial

-- 
Gabriel Genellina




More information about the Python-list mailing list