I see a article about the immutable object.
It says when:
variable = immutable
As assign the immutable to a variable.
for example
a = b # b is a immutable
It says in this case a refers to a copy of b, not reference to b.
If b is mutable, the a wiil be a reference to b
so:
a = 10
b = a
a =20
print (b) #b still is 10
but in this case:
a = 10
b = 10
a is b # return True
print id(10)
print id(a)
print id(b) # id(a) == id(b) == id(10)
if a is the copy of 10, and b is also the copy of 10, why id(a) == id(b) == id(10)?
While that article may be correct for some languages, it’s wrong for Python.
When you do any normal assignment in Python:
You aren’t making a copy of anything. You’re just pointing the name at the object on the right side of the assignment.
Mutability is irrelevant.
More specifically, the reason:
is
True, is that10is interned — meaning Python keeps one10in memory, and anything that is set to10points to that same10.If you do
You’ll get
False, butwill still be
True.