Python's shades of Copy

Source: Internet
Author: User
Tags shallow copy

Copy is a copy, what is the depth of the word?

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.

Actually, this is the result of shared memory.

Copy: The principle is to separate the data, copy its data, and later modify the non-impact.

Let's look at a non-copy example.

= Assignment: Data is fully shared (= assignment is pointing to the same object in memory, if it is a variable (mutable) type, such as a list, modify one, and the other must change

If the immutable type (immutable), such as a string, modifies one, the other does not change

 L1 = [1, 2, 3, [ " aa  " ,  " bb  "  ]]l2  = l1l2[0]  = " aaa  "  Span style= "COLOR: #000000" >l2[ 3][0]= "  BBB   " print  (L1) #  [' aaa ', 2, 3, [' BBB ', ' BB ']]   Print  (ID (L1) ==id (L2)) #  true  

L2 = L1, L1 is fully assigned to L2, L2 has the same memory address as L1, that is, memory is pointing completely

Shallow copy: Data semi-share (copy its data independent memory storage, but only copy the first layer of success)

1, complete slicing method, 2, factory function, such as list (); 3,copy module copy () function

L1 = [1,2,3,[11,22,33]]l2=l1.copy ()Print(L2)#[1,2,3,[11,22,33]]l2[3][2]='AAA'Print(L1)#[1, 2, 3, [One, one, ' aaa ']Print(L2)#[1, 2, 3, [One, one, ' aaa ']l1[0]=0Print(L1)#[0, 2, 3, [One, one, ' aaa ']Print(L2)#[1, 2, 3, [One, one, ' aaa ']Print(ID (L1) ==id (L2))#flase

As the above code, L2 a shallow copy of L1, then L2 the list of its list of elements to modify, from the results see, L1 has also been modified. But just modifying the first element of the L1 list does not affect L2.

Compare the memory address of L2 and L1: False, indicating that L2 in memory has been partially copied L1 data, but only a shallow copy, the second layer of data is not a successful copy, but point to the second layer of data in the memory address of the L1, so the shared memory ' equals ' equal value assignment ', So there will be a change in the second layer of data in the L2, and the second layer of data in L1

, this is the principle of shallow copy, L2 copy L1 only the first layer, that is, in other memory to recreate the L1 of the first layer of data, but L2 can not copy the L1 of the second layer of data, that is, the list of the list, so he can only point to the second layer of data in the L1

Thus, when the second layer of data in the L1 is modified, the second layer of data in a shallow copy of the L1 is changed accordingly.

Deep copy: Data is not shared at all (copying its data completely independent of one memory, full copy, data not shared)

A deep copy is a completely duplicated copy, and the data does not interact with each other because the memory is not shared.

The deep copy method has

Import Module

Import= [1, 2, 3, [one, all]]#  L2 = copy.copy (L1)  shallow copy L2 = Copy.deepcop Y (L1)print(L1,'>>>', L2) l2[3][0] = 1111Print (L1,">>>", L2)

This shows that the deep copy is the data completely independent copies of a copy. Will not change from the original data

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.