Cant set attribute with new-style properties in Python
Cant set attribute with new-style properties in Python
The documentation says the following about using decorator form of property
:
Be sure to give the additional functions the same name as the original property (x in this case.)
I have no idea why this is since if you use property
as function to return an attribute the methods can be called whatever you like.
So you need to change your code to the following:
@x.setter
def x(self, value):
setting
self._x = value
The setter method has to have the same name as the getter. Dont worry, the decorator knows how to tell them apart.
@x.setter
def x(self, value):
...
Cant set attribute with new-style properties in Python
When you call @x.setter, @x.getter, or @x.deleter, youre creating a new property object and giving it the name of the function youre decorating. So really, all that matters is that the last time you use a @x.*er decorator in the class definition, it has the name you actually want to use. But since this would leave your class namespace polluted with incomplete versions of the property you wish to use, its best to use the same name to clean them up.