class abc :
x = 10
list = []
def __init__(self):
self.a = 30
self.b = 40
a = abc()
b = abc()
a.x = a.x + 1
print a.x
print b.x
a.list.append(1)
print b.list
Output :
10
11
[1]
So we see that x
is not shared across objects a
and b
but list
is shared. Can someone explain this behaviour?
So its appears answer to this lies in the fact that list are mutable objs and numbers are not:
class abc :
x = 10
m_list = []
def __init__(self):
self.a = 30
self.b = 40
a = abc()
b = abc()
print id(a.x)
a.x = a.x + 1
print id(a.x)
print a.x
print b.x
print id(a.m_list)
a.m_list.append(1)
print id(a.m_list)
print b.m_list
print id(b.m_list)
output :
5342052
5342040
11
10
38600656
38600656
[1]
38600656
but this is so strange ...numbers are immutable ?