Python Copy objects (deep copy deepcopy and light copy copy) _python

Source: Internet
Author: User
Tags shallow copy
1. Copy.copy a shallow copy copies only the parent object and does not copy the inner child objects of the object.
2. Copy.deepcopy deep Copy object and its child objects
A good example:
--> ImportCopy
A=[1,2,3,4, ['A','B']]#Original Object

B=A#Assignment, passing a reference to an object
C=Copy.copy (a)# object copy, shallow copy
D = copy.deepcopy (a) # object copy, deep copy

A.append (5) # Modify Object A
a[4].append ('C') # Modify the [' A ', ' B '] Array object in Object a

Print 'a = ', a
Print 'b = ', b
Print 'c = ', C
print 'd = ', D

Output results:
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 ']]
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.