Transferred from: http://www.jb51.net/article/15714.htm
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
A good example:
ImportCopya= [1, 2, 3, 4, ['a','b']]#Original Objectb= A#assignment, a reference to a passing objectc = Copy.copy (a)#object Copy, shallow copyD = Copy.deepcopy (a)#object Copy, deep copyA.append (5)#Modify Object AA[4].append ('C')#Modify the [' A ', ' B '] Array object in Object aPrint 'A =', aPrint 'B =', bPrint 'C =', CPrint '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 ']]
Python Copy objects (deep copy deepcopy and shallow copy copy)