When assigning values between objects in Python is passed by reference, the copy module in the standard library is required if the object needs to be copied.
1. Copy.copy a shallow copy copies only the parent object and does not copy the inner sub-objects of the object.
2. copy.deepcopy deep copy copy objects and their sub-objects
1 ImportCopy2A = [1, 2, 3, 4, ['a','b']]#Original Object3 4b = A#assignment, a reference to a passing object5c = Copy.copy (a)#object Copy, shallow copy6D = Copy.deepcopy (a)#object Copy, deep copy7 8A.append (5)#Modify Object A9A[4].append ('C')#Modify the [' A ', ' B '] Array object in Object aTen One Print 'A =', a A Print 'B =', b - Print 'C =', C - Print 'd ='D
Output Result:
A = [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
b = [1, 2, 3, 4, [' A ', ' B ', ' C '], 5]
c = [1, 2, 3, 4, [' A ', ' B ', ' C ']]
D = [1, 2, 3, 4, [' A ', ' B ']]
On the other hand, you determine whether the object is a copy, and you can use the IS operator to identify it.
A is B, True A and B refer to the same object, not copy
-A and B are copying objects to each other
Python Learning copy Module