Python assignment, shallow copy, and deep copy

Source: Internet
Author: User
Tags shallow copy

Beginner python, and C + + is still a lot different. Direct assignment, shallow copy, and deep copy, there are still a number of differences between the operations of these three copy objects. The Python language version is 2.7 and is tested in Pycharm.

First, directly assigned value

Use the following code to experiment with:

1Origin = [1,"string", [1, 3, 5]]2Copy =Origin3 PrintCopy4 PrintID (origin), id (Copy)5Copy[0] = 56 Printorigin, Copy7COPY[1] ="changed"8 Printorigin, Copy9Copy[2][0] = 111Ten Printorigin, Copy One PrintID (origin), id (Copy)

The results of the operation are as follows:

[1,'string', [1, 3, 5]]38994824 38994824[5,'string', [1, 3, 5]] [5,'string', [1, 3, 5]][5,'changed', [1, 3, 5]] [5,'changed', [1, 3, 5]][5,'changed', [111, 3, 5]] [5,'changed', [111, 3, 5]]38994824 38994824

As can be seen, a new variable directly assigned is completely a reference to the original object, and any reference to the copied object will affect the original object.

Second, shallow copy

Use the same code to test, just change the copy mode to Copy.copy ():

1 ImportCopy2Origin = [1,"string", [1, 3, 5]]3Copy =Copy.copy (Origin)4 PrintCopy5 PrintID (origin), id (Copy)6Copy[0] = 57 Printorigin, Copy8COPY[1] ="changed"9 Printorigin, CopyTenCopy[2][0] = 111 One Printorigin, Copy A PrintID (origin), id (Copy)

The results of the operation are as follows:

[1,'string', [1, 3, 5]]39453768 39510280[1,'string', [1, 3, 5]] [5,'string', [1, 3, 5]][1,'string', [1, 3, 5]] [5,'changed', [1, 3, 5]][1,'string', [111, 3, 5]] [5,'changed', [111, 3, 5]]39453768 39510280

This time, it can be found that two objects point to the same memory, that is, a shallow copy of the object is a new object. In addition, it can be found that replacing the elements of a new object does not affect the original object, and the modification of the sub-object-list will affect the original object.

Third, deep copy

In the same way, just replace the copy with Copy.deepcopy ():

1 ImportCopy2Origin = [1,"string", [1, 3, 5]]3Copy =Copy.deepcopy (Origin)4 PrintCopy5 PrintID (origin), id (Copy)6Copy[0] = 57 Printorigin, Copy8COPY[1] ="changed"9 Printorigin, CopyTenCopy[2][0] = 111 One Printorigin, Copy A PrintID (origin), id (Copy)

The results are as follows:

[1,'string', [1, 3, 5]]39978056 39994504[1,'string', [1, 3, 5]] [5,'string', [1, 3, 5]][1,'string', [1, 3, 5]] [5,'changed', [1, 3, 5]][1,'string', [1, 3, 5]] [5,'changed', [111, 3, 5]]39978056 39994504

Similarly, two objects point to a different memory location, indicating that a new object was created. In addition, any changes to the new object do not affect the original object.

Conclusion:

(1) The direct assignment is a complete reference, and any changes to the new variable will affect the original object.

(2) A shallow copy creates a new object, but only copies the elements of the sequence, and for the element is also a sequence of cases (that is, sub-objects), only copy the reference to this sequence!

(3) A deep copy is a complete copy of the original object completely copied to the new object.

Python assignment, shallow copy, and deep copy

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.