Python Copy objects (deep copy deepcopy and shallow copy copy)

Source: Internet
Author: User

Http://www.jb51.net/article/15714.htm

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:

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) the   - #Run 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 ']] A " "

Python Copy objects (deep copy deepcopy and shallow copy copy)

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.