Python shallow copy and deep copy

Source: Internet
Author: User
Tags shallow copy

When the redemption code is written today, when the player redeems the redemption code, he gets the reward for the last redemption code, and has always suspected that the link to the redemption code in MySQL has gone wrong, actually referencing the data in the global constants directly. The data that causes the subsequent global constants uses the reward content of the last redemption code.

Let's talk about the first copy and the deep copy today!

Shallow copy:

Copy.copy: Copy Content

A shallow copy generates a new object, but still uses the reference (address) of the original data, uses a new object for the mutable type operation , does not produce a new object (LIST,DICT) for the immutable type operation, and modifies the corresponding value.

Deep copy:

Copy.deepcopy: Full Copy Content

A deep copy generates a new object, and the elements in the original object are regenerated.

--------------------------------------------------------------

The difference between shallow and deep copying are only relevant for compound objects (objects, contain other Obje CTS, like lists or class instances):

The difference between a shallow copy and a deep copy is only related to composite objects (objects that contain other objects, such as lists or class instances):

    • shallow copy  constructs a new compound object and then (to the extent possible) Inserts  references  into it to the objects found in The original.
    • < Span class= "Apple-converted-space" a shallow copy constructs a new composite object and then inserts (to the extent possible) a reference to its object inserted in the original object.
    • a&NBSP; deep copy  constructs a new compound object and then, recursively, Inserts&NBSP; copies  into it of the objects found In the Origin
    • deep copy constructs a new composite object, Then recursively insert it into its

The problems often exist with the deep copy operations that don ' t exist with shallow copy operations:

    • Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a Recursive Loop.
    • Because deep copy copies everything It could copy too much, e.g., administrative data structures that Shoul D be shared even between copies.

Python shallow copy and deep copy

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.