If an object's value can be modified, the object was said to be mutable. If The value cannot be Modified,the object is said to be immutable.
mutable mutable types, such as List,set, are custom types (equivalent to reference types in C #);
Immutable immutable types, such as string,numbers (equivalent to value types in C #);
I. References and copies (references and copies)
When using the = assignment operator in a program, such as A=b,
For immutable objects, A is created as a copy of B, A and B will point to different memory addresses, and A and b are independent of each other.
The code is as follows:
Def testcopy ():
A = 10
b = A
A =20
Print (b) #b still is 10
However, for mutable objects, a as a reference to B is created, the elements of A and B share the same memory address, and the elements of A and B are shared.
The code is as follows:
Def testref ():
a=[1,2,3,4]
B=a #b is a reference to a
Print (b is a) # True
B[2] = -100 #change an element in B
Print (a) # A also changed to [1,2,-100,4]
Second, deep copy and shallow copy (shallow copy and depth copy)
To prevent mutable objects from pointing to the same object, you must create a new copy, not a reference.
In Python, you can use two copies of container objects, such as lists and dictionaries, in shallow and deep copies.
A shallow copy creates a new object, but populates the new object with a reference to the element of the original object (if it is an immutable type, or a copy). You can use Copy.copy () to implement a shallow copy.
The code is as follows:
Def testshallowcopy ():
A = [1, 2, [3,4]]
b = List (a) # Create a shallow copy of a
Print (b is a) #False
B.append (+) #append element to B
Print (b)
Print (a) # A is unchanged
B[2][0] = -100 # Modify an element inside B
Print (b)
Print (a) # A is changed
In this example, A and B share the same mutable elements. So modify the elements in one of the list objects, and the other list object will be modified.
A deep copy creates a new object and recursively copies all of the elements contained in the object. You can use Copy.deepcopy () to implement a deep copy.
The code is as follows:
Def testdeepcopy ():
Import Copy
A = [1, 2, [3, 4]]
b = Copy.deepcopy (a)
B[2][0] = 100
Print (b) # B is changed
Print (a) # A is unchanged
In this example, A and B are opposing list objects, and their elements are independent of each other.
Iii. reference counting and garbage collection
All objects in Python are reference counts, and when an object is assigned or added to a container, its reference count is self-increasing, and when a del or variable is assigned to a different value, the reference count is reduced, and when the reference count is 0 o'clock, the python garbage collector reclaims the variable.
The code is as follows:
Def testgarbagecollection ():
Import Sys
Print (Sys.getrefcount (37))
A = PNS # creates an object with value 37
Print (Sys.getrefcount (37))
b = A # Increases reference count on 37
Print (Sys.getrefcount (37))
c = []
C.append (b) # increases reference count on 37
Print (Sys.getrefcount (37))
Del A # Decrease reference count of 37
Print (Sys.getrefcount (37))
b = # Decrease reference count of 37
Print (Sys.getrefcount (37))
C[0] = 2.0 # Decrease reference count of 37
Print (Sys.getrefcount (37))
Testgarbagecollection ()
The result of the operation is:
The code is as follows:
11
12
13
14
13
12
11
Why is there 11 citations on the first? Who knows?