Python copy object (deep copy deepcopy and light copy)
Source: Internet
Author: User
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']
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.