0
According to some examples I found I’m trying to implement an override of a property
, but the override does not work. I think this is a theme still a little obscure with few examples and little information about it. Follow my example:
from abc import ABCMeta, abstractmethod
class Person(metaclass=ABCMeta):
@abstractmethod
def __init__(self, name, age):
self.__name = name
self.age = age
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
self.__name = value
class Myself(Person):
def __init__(self, name, age, tel):
super().__init__(name, age)
self.tel = tel
@Person.name.setter
def name(self, value):
super().name = 'override'
class Wife(Person):
def __init__(self, name, age, tel):
super().__init__(name, age)
self.tel = tel
ms = Myself('Matheus Saraiva', 36, '988070350')
wi = Wife('Joice Saraiva', 34, '999923554')
print(ms.name)
If my implementation is correct, the result of print
should be:
>>> override
but the result is being:
>>> Matheus Saraiva
That is, apparently the override is not working. What’s wrong with my implementation?
I just realized that the result is being this as the initializer of
Person
is not going through theproperty
, instead I am assigned the value directly to the variableself.__name = name
, however, if I change toself.name = name
I get the error saying 'super' does not have an attribute 'name'.– Matheus Saraiva