Python allocates objects in the heap into two categories: mutable objects and immutable objects. The so-called mutable object means that the object's contents are mutable, such as list. Immutable objects, in contrast, indicate that their contents are immutable.
Immutable object: Int,string,float,tuple
Variable object: list,dictionary
For global variables, mutable objects differ greatly from immutable objects.
I. Immutable objects
Because variables in Python hold object references, object references to variables are mutable for non-mutable objects, although the object itself is immutable. The use of such mechanisms can sometimes confuse people, and it seems that mutable objects change. As in the following code:
i =
i + = 2
From the above, the immutable object has not changed, it is still an immutable object, the change is just the creation of a new object, changing the object reference of the variable.
Take a look at the code below to show this.
Second, for variable objects
The content of its objects can vary. When the content of an object changes, the object reference of the variable does not change. As the following example.
m=[5,9]
m+=[6]
Python base: Python mutable objects and immutable objects