Python: shallow and deep copy operations

Source: Internet
Author: User

Assignment statements in Python do not copy objects, they create bindings between a target and an object. for collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. this module provides generic shallow and deep copy operations (explained below ).

Interface summary:

copy. copy ( x )

return a shallow copy of x .

copy. deepcopy ( x )

return a deep copy of x .

Exception Copy. Error

Raised for module specific errors.

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

    • AShallow copyConstructs a new compound object and then (to the extent possible) insertsReferencesInto it to the objects found in the original.
    • ADeep copyConstructs a new compound object and then, recursively, insertsCopiesInto it of the objects found in the original.

Two problems often exist with 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 CopiesEverythingIt may copy too much, e.g., administrative data structures that shoshould be shared even between copies.

TheDeepcopy ()Function avoids these problems:

    • Keeping a "Memo" Dictionary of objects already copied during the current copying pass; and
    • Lew.user-defined classes override the copying operation or the set of components copied.

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. it does "copy" functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated byPickleModule.

Shallow copies of Dictionaries can be made usingDict. Copy (), And of lists by assigning a slice of the entire list, for example,Copied_list = Original_list [:].

Changed in version 2.5:Added copying functions.

Classes can use the same interfaces to control copying that they use to control pickling. See the description of ModulePickleFor information on these methods.CopyModule does not useCopy_regRegistration Module.

In order for a class to define its own copy implementation, it can define special methods_ Copy __() And _ Deepcopy __() . The former is called to implement the shallow copy operation; no additional arguments are passed. the latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. if _ Deepcopy __() Implementation needs to make a deep copy of a component, it shocould callDeepcopy () Function with the component as first argument and the memo dictionary as second argument.

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.