In below code in test() method self.name is None. I'm expecting it as 'B- Stack'. Am I missing something?
class A:
def __init__(self):
self.__name = None
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
self.__name = "A- " + value
class B(A):
@A.name.setter
def name(self, value):
self.__name = "B- " + value
def test(self):
print(self.name) # It should print B- Stack
b = B()
b.name = "Stack"
b.test()
you are printing self.name
which is setter method not actual self.__name
.
Either you can add getter method or print(self.__name)
in test method:
you can add getter method in B()
@name.getter
def name(self):
return self.__name
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.