Objects in Python are assigned values by reference. To copy objects, use the copy module in the standard library.
1. Copy. Copy: only the parent object is copied, and the internal sub-objects of the object are not copied.
2. Copy. deepcopy: Deep copy object and its sub-objects
A good example: Import Copy
A = [ 1 , 2 , 3 , 4 ,[ ' A ' , ' B ' ] # Original object
B = A # Assign values to pass object references
C = Copy. Copy () # Object copy and shortest copy
D = Copy. deepcopy () # Object copy, deep copy
A. append ( 5 ) # Modify Object
A [ 4 ]. Append ( ' C ' ) # Modify the ['A', 'B'] array object in object
Print ' 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']
Python daily delicious series (total)
Python daily delicious (20)-command line parameter SYS. argv
Python daily delicious (21)-httplib, smtplib
Python daily delicious (22)-copy object (deep copy deepcopy and light copy)
Python daily delicious (23)-enumerate traversing Arrays
Python daily delicious (24)-initializing multi-dimensional arrays
...