python--assignment, shallow copy, deep copy

Source: Internet
Author: User

Like many languages, Python is divided into simple assignments, shallow copies, and deep copies of these "copy" methods.

In the learning process, the initial understanding of the shallow copy is very vague. But after a series of experiments, I found that the concepts of these three are further understood.

First, assign a value

  The assignment is the most common of the three operations, and we analyze the assignment by some examples:

  STR example

' Hello ' ' Hello '>>> c = a>>> [ID for in a,b,c][  440412000044041200004404120000]

From the above instruction, we can find the address of A, B and C are the same. So the operation of the above assignment is equivalent to c = a = b = ' Hello '.

Assignment is that the system assigns memory to a variable or object (here is ' hello ') and assigns the address to A, B, C. So their addresses are the same.

 List example

>>> a = ['hello']>>> b = ['hello' ]>>> c = a>>> [ID for in a,b,c][  440397595244040950964403975952]

But this is not the case, the address of A and B are different. Why?

  Because STR is immutable, it is also ' hello ' with only one address, but the list is mutable, so two addresses must be assigned.

At this point, we would like to explore the above two cases if the value of the change ?

  STR example

'  World '>>> [ID -in a,b,c][44041204324404120000  4404120000]>>> print A, b, cworld hello hello

At this point the address and value of a change, but B, the C address and value are unchanged. Because of the immutability of STR, a re-assignment requires a re-opening of the memory space, so the value of a changes, and a points to address changes. b, c because of ' hello ' invariance, will not change.

  List example

 >>> a[0 ] =  " world   " >>> [id  (x)" Span style= "color: #0000ff;" >for  x  a,b,c][ 4403975952 , 4404095096 , 4403975952   " >>> print a, B, c[  '  world   ' ] [ hello   ' ] [ world   ' ] 

At this point a, the value and address of C change, but the two are still the same, B does not change. Because of the variability of the list, changing the value of the list does not require additional space, just modify the value of the original address. So a, c all change.

After understanding the different points above, we can analyze the shallow copy and deep copy very well.

We all use list as an example.

  Second, shallow copy

1>>> a = ['Hello', [123,234]]2>>> B =a[:]3>>> [ID(x) forXinchb]4[4496003656,4496066752]5>>> [ID(x) forXinchA]6[4496091584,4495947536]7>>> [ID(x) forXinchb]8[4496091584,4495947536]

line3,4 can see A, B address is different, this conforms to the list is variable, should open up different space. Does that shallow copy copy a copy? Looking at line5-8, we find that the address of the element in A, B is the same. If the string ' Hello ' address is consistent and understandable, but the second element is the list address is still the same. This illustrates the feature of a shallow copy, just a copy of the address of the element within the container .

We then try to modify the values in A, B:

1>>> a[0] =' World'2>>> a[1].append (345)3>>> Print'A ='A'\n\r','B =', b4A = [' World', [123,234,345]] 5b = ['Hello', [123,234,345]]

The first element in a str changes, but B does not change; a the second element changes, and B also changes. This is consistent with immutable object modifications that will open up new space, and mutable object modifications will not open up new space. It is further proved that a shallow copy simply replicates the address of the element in the container .

  Second, deep copy

1>>>From copy import deepcopy2>>> a = ['Hello', [123,234]]3>>> B =Deepcopy (a)4>>> [ID(x) forXinchA, b]5[4496066824,4496066680]6>>> [ID(x) forXinchA]7[4496091584,4496067040]8>>> [ID(x) forXinchb]9[4496091584,4496371792]

Deep copy, you can find a, B address and A, b in the element address are different. This is a copy that is completely copied.

After modifying the value of a:

1>>> a[0] =' World'2>>> a[1].append (345)3>>> Print'A ='A'\n\r','B =', b4A = [' World', [123,234,345]] 5b = ['Hello', [123,234]]

From the line4,5 you can see that only a has changed and B has not changed. because B is a complete copy, the element address is different from a, a modifies, and b is unaffected.

  Summarize:

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. A shallow copy creates a new variable or container in another address, but the address of the element within the container is a copy of the address of the element of the source object. This means that the new container points to the old element ( new bottle of Old wine ).

3. A deep copy is a new variable or container created in another address, and the address of the element within the container is also newly opened, just the same value, which is a complete copy. That means (new bottle of new wine ).

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