I have trouble understanding the logic of instance variables in inheritance. This is my (simplified) code with comments explaining how I understand its behaviour:
class Main(object):
def __init__(self):
self.p = Parent() # self.parameter = []
self.c = Child() # self.parameter = []
def run(self):
self.p.setting() # assigning value to self.parameter
self.c.getting()
class Parent(object):
def __init__(self):
self.parameter = []
def setting(self):
self.parameter = [1, 2, 3]
class Child(Parent):
# not redefining __init__, so Parent __init__ is called
def getting(self):
# value was assigned to self.parameter in setting method,
# called before getting
print self.parameter
Main().run()
getting prints [], instead of [1, 2, 3] which I expected. Why is it so? Since Child shares __init__ with Parent, at the beginning self.parameter = [] for both but why is it still [] when it was assigned a value long after Child().__init__ was called? What should I do to get changed self.parameter value in getting?
Main.