Why immutable objects are immutable

Immutable object

There is only one address in the memory , no matter how many references point to it, there is only one address value, but there is a reference count that records how many references point to this address .

But when the tuple is stored in the variable (that is, the memory address). So when the  variable pointing to the object changes, if the variable changes (that is, the immutable type), then the tuple guarantees that the immutable type cannot be modified . And if when the variable pointing to the object changes, if it does not cause the variable to change (that is, the variable type), the variable type stored in the tuple can be modified at this time (because the variable itself has not changed)

 

The advantage of the immutable data type is that no matter how many references there are in the memory , the same object only occupies a piece of memory , but its disadvantage is that when the variable needs to be operated to change the value of the object referenced by the variable, it is immutable Data type, so you must create new objects, which will make changes again and again to create a new object, but the memory that is no longer used will be recycled by the garbage collector.

Two variable objects

The variable data type means that when a variable is operated, its value is variable, and the change of the value will not cause a new object, that is , an object with the same value for a variable data type that does not change. It is a different object, that is , multiple objects with the same value are stored in the memory, and the address value is different

Add operations to the list, respectively a.append(4) and a += [2]. It is found that these two operations make the value of the object referenced by a become the final result above, but the address referenced by a is still 41575088, also That is to say, the operation performed on a will not change the value of the address referenced by a, but the new address is expanded after the address, and the value stored in the address is changed, so the variable data type means that when a variable is operated , Its value is variable, and the change of the value will not cause a new object, that is, the address will not change , but the content in the address has changed or the address has been expanded. The operation of the variable data type cannot be a new assignment operation directly , for example, a = [1, 2, 3, 4, 5, 6, 7], such an operation is not to change the value, but to create a new one New object , the variable here is only for operations like append, +=, etc.

 

Three summary

Whether to change the value of the variable to create a new object, and for the object of the same value, there is only one object in the memory, and there will be a reference count inside to record how many variables refer to this object;

Is changing the value of a variable to open up new memory space, is it to create a new object, invariant and mutable type, and variable and immutable type?

The memory address is just a number, representing a memory space, to be continued

Guess you like

Origin blog.csdn.net/weixin_42322206/article/details/111657454