My question is best explained with an example:
a = 1
b = a
a = a + 1
print(a,b)
# result is: 2 1
a = [1,2]
b = a
a.append(3)
print(a,b)
# result is: [1, 2, 3] [1, 2, 3]
I'm trying to understand the logic or at least the rule behind this behavior.
Why are a
and b
linked when the original object is a list()
, but not when it is a int
? And in general, what are the types of objects that are linked/ not linked? Is there a general rule?