[PYTHON] Depth resolution copy.copy () and Copy.deepcopy ()

Source: Internet
Author: User

In the list, you want to copy a list, you can use the copy () function, in the list, the copy () function is the same as the slice [:] function

a=[0,1,[0,1]]

B=a.copy ()

c=a[:]

-----Print B,c-----

b=[0,1,[0,1]]

c=[0,1,[0,1]]

Copy.copy () also has the same function in copy module, but there is a function in the copy module that is similar to copy.copy (), but functions differently, that is copy.deepcopy ()

This is the protagonist of today's ' Shallow copy copy.copy () ' and ' deep copy copy.deepcopy () '

The difference between the copy module and the copy () function in the list is first shown by a single graph.

Copy () in the visible list of this graph is not only the same as the [:] Copy.copy () function

But the two functions in the Copy module are missing.

First look at the principle of ' shallow copy copy.copy () '

The Python visualization process shows that in ' shallow copy ', B does not store a small list in the list to a new address, but rather a common address with a, so when a small list of values changes, B is the same.

Again, the ' deep copy copy.deepcopy () ' principle

As the graph shows, ' deep copy ', B stores a small list of oranges in a new address, regardless of the value of A's small list, but does not affect the small list of B.

Thanks for reading

Or a student, in the text is likely to appear wrong ideas or terminology, please senior criticism and guidance, thank you!

[PYTHON] Depth resolution copy.copy () and Copy.deepcopy ()

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.