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

Source: Internet
Author: User
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:

Import Copya = [1, 2, 3, 4, [' A ', ' B ']]  #原始对象b = a  #赋值, reference to the object c = Copy.copy (a)  #对象拷贝, shallow copy d = Copy.deepcopy (a) 
  #对象拷贝, deep copy a.append (5)  #修改对象aa [4].append (' C ')  #修改对象a中的 [' A ', ' B '] Array object print ' A = ', aprint ' B = ', bprint ' C = ', CP Rint ' 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 ']]

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.