This is a piece of Python code to learn inheritance.
class Animal():
__name= None
__sound = None
def __init__(self, name , sound):
self.__name= name
self.__sound = sound
def ToString(self):
print ("The {} has the sound {}".format(self.__name ,
self.__sound))
cat = Animal('Tiger' , 'roars')
cat.ToString()
class Panther(Animal):
__owner = None
def __init__(self , name ,sound ,owner):
self.__owner = owner
super(Panther ,self).__init__(name, sound)
def ToString(self):
print(self.__owner)
print(self.__name)
leopard = Panther('Leopard' , 'roars' , 'Senegal')
leopard.ToString()
But when I try to run it in Pycharm, I get the following error:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/venkat/PycharmProjects/PythonOneVideo/main.py The Tiger has the sound roars Senegal Traceback (most recent call last): File "/Users/venkat/PycharmProjects/PythonOneVideo/main.py", line 41, in leopard.ToString() File "/Users/venkat/PycharmProjects/PythonOneVideo/main.py", line 35, in ToString print(self.__name) AttributeError: 'Panther' object has no attribute '_Panther__name'
Process finished with exit code 1
What's wrong with calling the super class constructor? Why this error has happened and how to solve this? Any help would be appreciated.