I'm trying to use @property and @propertyName.setter to get / set values on different properties but face the following issue when i chain multiple accessors.
This is a simple example i could reproduce my issue:
class Toto:
def __init__(self):
self._name = None
self._label = None
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
class Tata:
def __init__(self):
self._toto = Toto()
@property
def toto(self):
return self._toto
@toto.setter
def toto(self, value):
self._toto = value
When i execute this:
tata = Tata()
print("Toto name is", tata.toto.name)
tata.toto.name("titi")
print("Toto name is", tata.toto.name)
I obtain the following error:
Toto name is None
Traceback (most recent call last):
File "/home/radjanidar/projects/music/demo/src/main/script/chainedproperties.py", line 43, in <module>
tata.toto.name("titi")
TypeError: 'NoneType' object is not callable
Why can't i use the setter method on object Toto?
That's not how you use properties. Basically, properties are ways to make function calls act like attributes. When getting and setting them, the decorated getters and setters respectively are called, instead of performing naive attribute lookup.
Accordingly, you want tata.toto.name = 'titi'
.
You've defined name as a property which changes how you interact with it.
Instead of tata.toto.name("titi")
, you use tata.toto.name = "titi"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.