mutable type Vs immutable type (variable immutable here, refers to whether that piece of content in memory (value) can be changed. )
- mutable type (mutable): list, dictionary
- Immutable types (unmutable): Numbers, strings, tuples
- In the case of an immutable type, when manipulating the object itself, a new area must be requested in memory (because the old region # is immutable #). The value of the variable is not allowed to change, if the value of the variable is changed, the equivalent of a new object, and for the same value of the object, in memory there is only one object, there is a reference count to record how many variables to refer to the object, when the reference calculation becomes 0, the garbage collection mechanism is enabled, The original address is no longer present.
- In the case of mutable types, when manipulating objects, there is no need to apply memory elsewhere, where the address does not change and the value can change. For objects of the same value, there are different objects in memory, that is, each object has its own address, which is equivalent to how many copies of memory are stored for the same value object, there is no reference count, and it is a real object.
- You can use the built-in function ID () to confirm whether an object's identity has changed before and after two assignments.
Python mutable type Vs immutable type