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

petr.jakes.tpc at gmail.com petr.jakes.tpc at gmail.com
Mon Feb 25 15:50:20 EST 2008


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?

regards

Petr Jakes



More information about the Python-list mailing list