Python's shades of Copy

Source: Internet
Author: User
Tags shallow copy

Defined:

The assignment of an object in Python is actually a reference to the object. When creating an object and assigning it to another variable, Python does not copy the object, only the reference to the object is copied.

Shallow copy: Copies the outermost object itself, and the internal elements are just copies of a reference. That is, the object is copied over again, but the other objects referenced in the object are just copy references.

Deep copy: Both the outer and inner elements are copied to the object itself, not the reference. That is, copy all the objects again.

Explanation of several terms:
    1. Variable: is an element of a system table that has a connection space to an object
    2. Object: The allocated piece of memory that stores the value represented by the
    3. Reference: is an automatically formed pointer from a variable to an object
    4. Immutable objects: Objects that cannot be modified as soon as they are created, including strings, tuples, numbers, and Boolean values
    5. mutable objects: Objects that can be modified, such as lists, dictionaries
The role of the shades of copy:

1, reduce the use of memory

2, in the cleaning, modification or storage of the data, the original data is copied to prevent data modification, the original data can not be found.

For a dark copy of an immutable object:

Immutable object type, not copied, even with a deep copy, the view ID is the same, if it is re-assigned, but also just re-create a new object, replace the old only.

In a word, an immutable type, whether deep or shallow, is the same as the value of the address and the copy.

Import Copy "Tu = (3,4,5) tu_1 = Copy.deepcopy (TU) #对于不可变对象, the same shade copy is the memory address tu_1 = (3,4,5) #值改变, the address will also change # print (ID (TU), id (tu_1)) #拷贝-Variable Object li_1 = [1,2,3,4,[5,6,7]]# ' = ' memory address, pointing to the same object li_2 = Li_1print (ID (li_1), ID (li_2)) # 2826200972232 2826200972232li_2[4].append (' 90000 ') print (li_1) #li_2发生了改变 li_1 will also change # copy-shallow copy only the first and second layer address references li_2 = Copy.copy (li_1) print (ID (li_1[4]), ID (li_2[4])) #里面第二层的list地址一样, proves that pointing to the same thing # only changes the outermost layer, does not affect the other li_1.append (' 789 ') Print (li_2)  # [1, 2, 3, 4, [5, 6, 7]] #如果改变第二层, the other will also change li_1[4].append (' changed things ') print (li_2) # [1, 2, 3, 4, [5, 6, 7, ' changed east West ']]# deepcopy-deep copy copy everything from all layers li_1 = [1,2,3,4,[5,6,7]]li_2 = copy.deepcopy (li_1) print (ID (li_1), ID (li_2)) # The first layer address is different print (ID (li_1[4]), ID (li_2[4])) #第二层地址不同 # The first li_1 change is not related to li_2 li_1[4].append (' li_1 has changed ') print (li_2) # [1, 2, 3, 4, [5, 6, 7]] "

For shades of variable objects:

' = ': Shallow copy: Equal value, equal address

Copy shallow copy: value is equal, address is not equal, second layer copy memory address, point to the same thing

Deepcopy deep Copy: The values are equal and the addresses are not equal.

Summarize:
    • A deep copy is a copy of the source object, taking up different memory space
    • An immutable type of object that has no effect on a dark copy, and the final address value and value are equal
    • mutable objects
      • ' = ': shallow copy, value equal, address equal
      • Copy shallow copy: value is equal, address is not equal, second layer copy memory address, point to the same thing
      • Deepcopy deep Copy: Equal values, unequal addresses, two independent objects

Python's shades of Copy

Related Article

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.