I have a problem with inheritance in Python.I have changed the member variable of ParentClass by itself.The problem is ChildClass cannot access to new value of ParentClass' member variable. Please look at this simple example:
class Parent(object):
def __init__(self):
self.name = "Tommy"
def changeParentName(self):
self.name = "Jack"
class Child(Parent):
def parentName(self):
print self.name
parent = Parent()
parent.changeParentName()
child = Child()
child.parentName()
If you try above example, you'll see this result:
Tommy
But i expect to see Jack instead of Tommy. I have this problem with Python 2.7.9 Can anyone explain this problem or give us a solution? Does ChildClass call ParentClass' Constructor? so self.name equals Tommy again. Actually, i have this problem in my project, But i have explained my problem with above example