Python mutable and immutable objects

Python mutable and immutable objects

1. Immutable object
Immutable object means that the value of the address pointed to by an object cannot be modified. If the value of this object is modified, the address pointed to by it will change. It is equivalent to copying the value pointed to by this object, and then saving it to another address after making changes. (If you want to change, find another place)

For immutable objects, calling any method of the object itself will not change the content of the object itself. These objects will create new objects and return them, thus ensuring that the immutable object itself is always immutable.
Insert picture description hereResult:
Insert picture description here
Insert picture description hereResult:
Insert picture description here

2. Variable object
Variable object means that an object can modify the value in the address it points to without changing the address it points to.
The value is changed directly at the address pointed to by the object, and the object still points to this address.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44994799/article/details/109577579