class base:
def __init__(self):
self.x = [1, 2, 3]
class child1(base):
def __init__(self):
super().__init__()
def __repr__(self):
return str(self.x) + ' '+ str(id(self.x))
class child2(base):
def __init__(self):
super().__init__()
def __repr__(self):
return str(self.x) + ' ' + str(id(self.x))
a = child1()
b = child2()
code | when self.x of base class mutable | when self.x of base class is not mutable |
---|---|---|
print(a) | [1, 2, 3] 140256922286752 | 1000 140256922840528 |
print(b) | [1, 2, 3] 140256964882096 | 1000 140256922840528 |
My question is:
integers which are not in range from -5 to 256, will have different id.
But in my case self.x of for the child classes is same as base. which means they point to the same location.
But with an mutable object like list it is different, my question is, are both the lists copies(shallow) of base class list, or are they same. Also in general for python, do child class variables point to the same location in memory where the base class variable point?
Thank you for helping me out.