I am new to python and I wasn't sure what I was doing was correct.
I have a base class A and an inherited class B.
class A(object):
def __init__(self, name):
self.__name = name
@property
def name(self):
return self.__name
@name.setter
def name(self, name):
self.__name = name
class B(A):
def __init__(self, name):
super(NominalValue, self).__init__(name)
@property
def name2(self):
return self.__name2
@name2.setter
def name2(self, n):
self.__name2 = n
def toString():
print self.__name + self.__name2
if __name__ == "__main__":
instance = B('name');
instance.toString()
When I run this it complains that class B does not have any attribute __name.
AttributeError: 'B' object has no attribute '_B__name'
I am clearly not doing the inheritance correctly. How do you treat properties to be correctly inherited and avoid repeating attributes in the inherited class?
__nameattribute I think you need to refer to it asself._A__name.