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

P

petr.jakes.tpc

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
 
G

Gabriel Genellina

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top