Python deep copy, shallow copy

Source: Internet
Author: User

In Python, there is a difference between the assignment of an object and the copy (deep/shallow copy), which can produce unexpected results if used without notice.

First of all, we should have the following knowledge about the assignment operation:

    1. Assignment is to assign the address of an object to a variable, so that the variable points to that address (old bottles of old wine).
    2. Modify immutable Objects ( str , tuple ) need to open up new space
    3. modifying mutable objects ( list etc.) no need to open up new space
Shallow copy

? A shallow copy copies only the addresses of the elements in the container, copies the references, and does not copy the contents.

In [1]: a = [11,22,33]In [2]: id(a)Out[2]: 22132096In [3]: b = aIn [4]: id(b)Out[4]: 22132096 # 引用相同In [5]: a.append(44)In [6]: aOut[6]: [11, 22, 33, 44]In [7]: bOut[7]: [11, 22, 33, 44] # b的值也变化
Deep copy

A deep copy is a copy (recursive) for all levels of an object

in [8]:ImportCopyIn [9]: A=[ One, A, -]in [Ten]:ID(a) out[Ten]:74598640in [ One]: b=Copy.deepcopy (b) in [ A]:ID(b) out[ A]:79142624in [ -]: A.append ( -) in [ -]: aout[ -]: [ One, A, -, -]in [ the]: bout[ the]: [ One, A, -, -]

Further understanding

in [ at]: A=[ One, A, -]in [ -]: b=[ -, -, the]in [ -]: C=(A, B) in [ -]: E=Copy.deepcopy (c) in [ -]: A.append ( the) in [ -]: aout[ -]: [ One, A, -, the]in [ in]: bout[ in]: [ -, -, the]in [ -]: cout[ -]: ([ One, A, -, the], [ -, -, the]) in [ to]: eout[ to]: ([ One, A, -], [ -, -, the])# Copy.copy () methodin [ +]: F=Copy.copy (c) in [ -]: A.append ( the) in [ the]: aout[ the]: [ One, A, -, the, the]in [ *]: bout[ *]: [ -, -, the]in [ $]: cout[ $]: ([ One, A, -, the, the], [ -, -, the]) in [Panax Notoginseng]: eout[Panax Notoginseng]: ([ One, A, -], [ -, -, the]) in [ -]: fout[ -]: ([ One, A, -, the, the], [ -, -, the])
Other ways to copy a shallow copy differs from a copy of a immutable type and a mutable type
in [ the]: A=[ One, A, -]in [ the]: b=Copy.copy (a) in [ -]:ID(a) out[ -]:59275144in [ the]:ID(b) out[ the]:59525600in [ the]: A.append ( -) in [ the]: aout[ the]: [ One, A, -, -]in [94]: bout[94]: [ One, A, -]in [ the]: A=( One, A, -) in [ the]: b=Copy.copy (a) in [ the]:ID(a) out[ the]:58890680in [98]:ID(b) out[98]:58890680
    • A shard expression can assign a sequence of values
    ="abc"    = a[:]
    • The copy method of the dictionary can copy a dictionary
    =dict(name="zhangsan", age=27)    = d.copy()
    • Some built-in functions can generate copies (list)
    =list(range(10))    =list(a)
    • copy function in the copy module
    import copy    = (1,2,3)    = copy.copy(a)

Python deep copy, shallow 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.