Python---The difference between copy (), Deepcopy () and assignment

Source: Internet
Author: User
Tags shallow copy

The copy between deepcopy is the entire value, and the deepcopy does not change when the original value changes

Array type copy, the copied value also changes, if it is a = [1,3] This full assignment, the original copy value will not change, if the value of each position of the array is assigned a new value (such as a[1] = 1,a[2] = 3)

The main difference between copy () and Deepcopy () is how Python stores the data.

First straight to the conclusion:

-deep copy, which will be copied completely again as a separate new entity exists alone. Therefore, changing the original copied object will not affect the new object that has been copied.

--and equal to the assignment, does not produce a separate object, he simply hits the original block with a new tag, so when one of the tags is changed, the data block changes and the other tag changes.

--and shallow replication is discussed in two different situations:

1) When the value of a shallow copy is an immutable object (numeric, string, tuple) and is equal to assignment, the object's ID value is the same as the light copy original value.

2) When the value of a shallow copy is a mutable object (list and tuple), a "not so independent object" exists. There are two types of situations:

The first case is that there are no complex sub-objects in the copied object, and the change of the original value does not affect the value of the shallow copy, and the value changes of the shallow copy do not affect the original value. The ID value of the original value differs from the original value of shallow copy.

The second case is that there are complex sub-objects in the copied object (for example, a child element in the list is a list), and if you do not change the complex sub-object, the value of the shallow copy changes without affecting the original value. However, changing the value of the complex sub-object in the original value affects the value of the shallow copy.

For simple object, such as immutable objects (numeric, string, tuple), there is no difference between shallow copy and deep copy

Complex object, such as the list in the case, shallow copy of the sub-list, not from the original object really "independent" out. In other words, if you change an element in the child list of the original object, your copy will change along with it. This is different from our intuitive understanding of "copy".

When the value of a shallow copy is an immutable object (numeric, string, tuple), the code is as follows:

    1. >>> a="1234567"
    2. >>> b=a
    3. >>> ID (a)
    4. 4367619440
    5. >>> ID (b)
    6. 4367619440
    7. >>> c=copy.copy (a)
    8. >>> ID (c)
    9. 4367619440
    10. >>> d=copy.deepcopy (a)
    11. >>> ID (d)
    12. 4367619440

When the value of a shallow copy is a mutable object (list, dictionary), the changed value is not the complex sub-object code as follows:

  1. >>> l1=[1,2,3]
  2. >>> l2=l1
  3. >>> l3=copy.copy (L1)
  4. >>> ID (L1)
  5. 4367654664
  6. >>> ID (L2)
  7. 4367654664
  8. >>> ID (L3)
  9. 4367628616
  10. >>> l1.append (in)
  11. >>> Print (L1)
  12. [1, 2, 3, + ]
  13. >>> Print (L3)
  14. [1, 2, 3]

When the value of a shallow copy is a mutable object (list, dictionary), the changed value is the complex sub-object code as follows:

  1. >>> Import copy
  2. >>> list1=[1,2,[' A ',' B ']
  3. >>> List2=list1
  4. >>> list3=copy.copy (list2)
  5. >>> list4=copy.deepcopy (LIST3)
  6. >>> ID (list1)
  7. 4338414656
  8. >>> ID (list2)
  9. 4338414656
  10. >>> ID (list3)
  11. 4338415016
  12. >>> ID (list4)
  13. 4338414368
  14. >>> list1[2].append (' a ')
  15. >>> ID (list1)
  16. 4338414656
  17. >>> Print List1
  18. [1, 2, [' a ', ' B ', ' a ']]
  19. >>> print list3
  20. [1, 2, [' a ', ' B ', ' a ']]
  21. >>> print list4
  22. [1, 2, [' a ', ' B ']]
  23. >>> list1.append (+)
  24. >>> ID (list1)
  25. 4338414656
  26. >>> ID (list3)
  27. 4338415016
  28. >>> Print List1
  29. [1, 2, [' a ', ' B ', ' a '], [ ]
  30. >>> print list3
  31. [1, 2, [' a ', ' B ', ' a ']]


Code Description: When changing elements in complex sub-objects, the shallow copy values have changed; When the changed value is not a complex sub-object, the value of the shallow copy does not change. Because of shallow copying, complex sub-objects are saved as a reference, so modifying the values of the shallow copy and the original values can change the values of the complex sub-objects.

How python data is stored

Python stores variables in a different way than other OOP languages. Instead of assigning a value to a variable, it is a reference to a specific value.

When in Python a = something should be understood to put a label on the something. When you assign a value to a, it's like taking a tag off the original something and pasting it onto other objects to create a new reference. This explains some of the strange things you might encounter in Python:

  1. >> A = [1, 2, 3]
  2. >>> B = a
  3. >>> a = [4, 5, 6]//assigns a new value to a
  4. >>> a
  5. [4, 5, 6]
  6. >>> b
  7. [1, 2, 3]
  8. # After the value of a has changed, B does not change with a
  9. >>> A = [1, 2, 3]
  10. >>> B = a
  11. >>> a[0], a[1], a[2] = 4, 5, 6//change the elements in the original list
  12. >>> a
  13. [4, 5, 6]
  14. >>> b
  15. [4, 5, 6]
  16. # After the value of a has changed, B changes with a

In both of the above code, the value of a has changed. The difference is that the first piece of code is directly assigned a new value (from [1, 2, 3] to [4, 5, 6]), and the second paragraph changes each element in the list individually. The effect on B is different, one does not change the value of B, and the other changes. How to use the above reason to explain the strange difference? For the first time, [1, 2, 3] is regarded as an object. A = [1, 2, 3] is equivalent to labeling the item A. B = A is a label for the item and a B.


The first case: a = [4, 5, 6] is equivalent to tearing a label from [1, 2, 3] and pasting it onto [4, 5, 6]. In this process, [1, 2, 3] This item does not disappear. b from start to finish well on [1, 2, 3], since this reference has not changed. The value of B does not change naturally.



The second case: A[0], a[1], a[2] = 4, 5, 6 is directly changed [1, 2, 3] the item itself. Re-modified every part of it inside. After the internal modification, [1, 2, 3] itself became [4, 5, 6]. And in the process, A and B are not moving, they are also affixed to the item. So the natural value of a B is changed to [4, 5, 6].

After figuring this out, I'm going to ask, what happened to a simple copy of a complex object in copy?
Look at the code again:

  1. >>> import copy
  2. >>> origin = [1, 2, [3, 4]]
  3. There are three elements inside #origin: 1, 2,[3, 4]
  4. >>> cop1 = Copy.copy (Origin)
  5. >>> cop2 = Copy.deepcopy (Origin)
  6. >>> Cop1 = = Cop2
  7. True
  8. >>> cop1 is cop2
  9. False
  10. #cop1 and Cop2 look the same, but are no longer the same object
  11. >>> origin[2][0] = "hey!"
  12. >>> Origin
  13. [1, 2, [' hey! ', 4]]
  14. >>> Cop1
  15. [1, 2, [' hey! ', 4]]
  16. >>> cop2
  17. [1, 2, [3, 4]]
  18. #把origin内的子list [3, 4] changed an element to observe COP1 and COP2

People who have studied Docker should be familiar with the concept of mirroring, and we can apply the concept of mirroring to copy.

The copy concept map is as follows:

Copy (shallow copy) for a complex object's sub-object is not completely copied, what is the sub-object of complex objects? Like nested sequences in a sequence, nested sequences in a dictionary are sub-objects of complex objects. For sub-objects, Python stores it as a public image, and all copies of it are treated as a reference, so that when one of the references changes the mirror and the other reference uses the mirror, the image is changed.

So look at the origin[2 here], that is [3, 4] this list. According to the definition of shallow copy, the cop1[2] points to the same list [3, 4]. Well, if we change this list here, it will cause both origin and cop1 to change at the same time. This is why the top origin[2][0] = "hey!", Cop1 also became [1, 2, [' hey! ', 4]].

The Deepcopy concept map is as follows:

Deepcopy will copy each layer of the complex object to a single individual.
This time origin[2] and cop2[2] Although the values are equal to [3, 4], but it is not the same list. That we replicate in the ordinary sense.

Python---The difference between copy (), Deepcopy () and assignment

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.