I am confused with variable sharing concept in Python in inheritance.
Consider the following code:-
class a(object):
var1 = 0
var2 = {}
def print_var(self):
print(self.var1)
print(self.var2)
class b(a):
@classmethod
def modify_var(cls):
cls.var1 = 1
cls.var2['temp']="something"
o1 = a()
o2 = b()
print("Initial Values")
o1.print_var()
o2.print_var()
print("Changing Values")
o2.modify_var()
print("Print values after change")
o1.print_var()
o2.print_var()
After running above code I can see the dictionary is being shared between child and parent class, but integer variable isn't.
Can anyone please explain this, or what I am doing wrong here?
Output of above code:
Initial Values
0
{}
0
{}
Changing Values
Print values after change
0 # <- this zero should be one according to my understanding
{'temp': 'something'}
1
{'temp': 'something'}