5
is immutable, too. When you have an immutable data structure, a += b
is equivalent to a = a + b
, so a new number, tuple or whatever is created.
When doing this with mutable structures, the structure is changed.
Example:
>>> tup = (1, 2, 3)
>>> id(tup)
140153476307856
>>> tup += (4, 5)
>>> id(tup)
140153479825840
See how the id
changed? That means it's a different object.
Now with a list
, which is mutable:
>>> lst = [1, 2, 3]
>>> id(lst)
140153476247704
>>> lst += [4, 5]
>>> id(lst)
140153476247704
The id
says the same.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…