Assuming you know about Python builtin property: http://docs.python.org/library/functions.html#property
I want to re-set a object property in this way but, I need to do it inside a method to be able to pass to it some arguments, currently all the web examples of property() are defining the property outside the methods, and trying the obvious...
def alpha(self, beta):
self.x = property(beta)
...seems not to work, I'm glad if you can show me my concept error or other alternative solutions without subclassing the code (actually my code is already over-subclassed) or using decorators (this is the solution I'll use if there is no other).
Thanks.
Properties work using the descriptor protocol, which only works on attributes of a class object. The property object has to be stored in a class attribute. You can't "override" it on a per-instance basis.
You can, of course, provide a property on the class that gets an instance attribute or falls back to some default:
class C(object):
_default_x = 5
_x = None
@property
def x(self):
return self._x or self._default_x
def alpha(self, beta):
self._x = beta
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With