Python's deep-down copy is finally clear.

Source: Internet
Author: User
Tags shallow copy

Python-deep copy is not always understood, just a conceptual understanding like what you said, knock out the code below and hopefully you'll understand.

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 copy objects and their sub-objects

>>> import copy>>> l1 = [1,3,5,[' A ', ' B ', ' C ']]>>> a = l1>>> B = Copy.copy (a) # shallow copy >& gt;> C = Copy.deepcopy (a) # deep copy >>> l1.append (7) >>> l1[3].append (' d ') >>> print ' l1= ', l1l1= [1, 3, 5, [' A ', ' B ', ' C ', ' d '], 7]>>> print ' a= ', aa= [1, 3, 5, [' A ', ' B ', ' C ', ' d '], 7]>>> print ' b= ', BB = [1, 3, 5, [' A ', ' B ', ' C ', ' d ']] >>> print ' c= ', cc= [1, 3, 5, [' A ', ' B ', ' C ']] # deep copy data no change

Note: The difference between a deep copy is a nested part, a shallow copy and the elements of the nested part of the copied object are the same,

A change of the other will also change, listed above example L1 = [1,3,5,[' A ', ' B ', ' C ']], the nested part is [' A ', ' B ', ' C '], where the data changes, the shallow copy will also change.


More detailed Reference Blog http://blog.csdn.net/jerry_1126/article/details/41852591

This article is from the "Sdsca" blog, make sure to keep this source http://sdsca.blog.51cto.com/10852974/1983766

Python's deep-down copy is finally clear.

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.