Each Python object is divided into mutable and immutable, the main core type, where numbers, strings, tuples are immutable, and lists and dictionaries are mutable.
Variable types and immutable types some places differ greatly, pay attention to differentiate.
The re-assignment of variables of immutable types is essentially recreating an object of immutable type and re-pointing the original variable to the newly created object (i.e., if no other variable references the original object (that is, the reference count is 0), the original object is recycled).
Immutable types
Take the int type as an example: actually i + = 1 is not really on the original int object +1, but instead re-create an int object with value 6, I refer to this new object.
You can see that the memory address will change when i + = 1 o'clock is executed because the int type is immutable.
Change the code, but the variable values of multiple int types are identical, to see if they have the same memory address.
For immutable type int, regardless of how many immutable types are created, as long as the values are the same, point to the same memory address. In the same situation there are short strings, floating-point types.
Variable type
Take a list of lists as an example
List after append, still point to the same memory address, because list is a mutable type, can be modified in place.
To change the code, when there are multiple variable type variables with the same values, see if they point to the same memory address as the immutable type.
As you can see from the running results, the values of A and B are the same, but the memory addresses that point to are different. We can also make them point to the same memory address by assigning a value of B = A:
This time to note, because A, B points to the same memory address, and a, B types are list, mutable type, a, b any list modification, will affect the value of another list.
mutable elements and immutable elements of elements in a class
Linux mutable types and immutable types