[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

petr.jakes.tpc

2/25/2008 8:50:00 PM

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
1 Answer

Gabriel Genellina

2/26/2008 7:13:00 AM

0

En Mon, 25 Feb 2008 18:50:20 -0200, <petr.jakes.tpc@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