Mutability
Some concepts
An object’s identity never changes once it has been created.
An object’s type defines the possible values and operations. It is unchangeable like the identity.
The value of some objects can change, depending on the type.
Some objects contain references to other objects, these objects are called containers (like tuple, list, or dictionary).
All variable names in Python are said to be references to the values.
Python keeps an internal counter on how many references an object has.
Once the counter goes to zero the GB in Python removes the object, thus freeing up the memory.
Immutable Objects
Every time when we try to update the value of an immutable object, a new object is created instead.
That’s when we have updated the first string it doesn’t change the value of the second. Immutable data types:
int
float
decimal
bool
string
tuple
range
Mutable Objects
list
dictionary
set
user-defined classes
Container objects
Some objects contain references to other objects, these objects are called containers (ie, tuple, list, or dictionary). The value of an immutable container that contains a reference to a mutable object can be changed if that mutable object is changed.
However, the container is still considered immutable because when we talk about the mutability of a container only the identities of the contained objects are implied.
Mutable Default Arguments
Related sources:
Last updated