Python assignment and depth copy

Source: Internet
Author: User
Tags shallow copy

Assignment value:
 >> A = [1, 2, 3 >>> b = a  >>> a = [4, 5, 6]// Assigns a new value to a  >>> a[
     4, 5, 6]  >>> b[ 1, 2, 3  #   b does not follow a when the value of a is changed. Change  >>> a = [1, 2, 3 >>> b = a< /span>>>> a[0], a[1], a[2] = 4, 5, 6// Change the elements in the original list  >>> a[ 4, 5, 6]  >>> b [ 4, 5, 6]  #   

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.

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.

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].

deep, shallow copy:
>>>ImportCopy>>> origin = [1, 2, [3, 4]]#There are three elements inside Origin: 1, 2,[3, 4]>>> COP1 =Copy.copy (Origin)>>> COP2 =Copy.deepcopy (Origin)>>> COP1 = =cop2true>>> COP1 isCop2false#Cop1 and Cop2 look the same, but they are no longer the same object>>> Origin[2][0] ="hey!">>>origin[1, 2, ['hey!', 4]]>>>cop1[1, 2, ['hey!', 4]]>>>cop2[1, 2, [3, 4]]#Change the Sub list in Origin [3, 4] to an element, observing Cop1 and COP2

Copy is not completely copied for a complex object's child objects, what is a child object of a complex object? 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]].

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.

Reference Http://iaman.actor/blog/2016/04/17/copy-in-python

Python assignment and depth 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.