Deep understanding of shallow copy and deep copy in python, and deep understanding of python copy

Source: Internet
Author: User

Deep understanding of shallow copy and deep copy in python, and deep understanding of python copy

Before talking about what is a copy of depth, let's look at this phenomenon:

a = ['scolia', 123, [], ]b = a[:]b[2].append(666)print aprint b

Why does it affect a if I only modify B? I have read it in my previous article and said: memory references are stored in the sequence.

Therefore, when we modify the empty list through B, it is actually modifying the same object in the memory, so it will affect.

a = ['scolia', 123, [], ]b = a[:]print id(a), id(a[0]), id(a[1]), id(a[2])print id(b), id(b[0]), id(b[1]), id(b[2])

Code verification is correct, so although a and B are two different objects, the references in them are the same. This is the so-called new object, the old content.

However, this is not the case for the shortest copy. See the following:

a = ['scolia', 123, [], ]b = a[:]b[1] = 666print aprint b

What is the problem?

I have read from my colleagues who have explained how to assign values to python variables: For immutable data types such as strings and numbers, modifying them is equivalent to re-assigning values. Here it is equivalent to refresh the reference.

Code Verification:

a = ['scolia', 123, [], ]b = a[:]b[1] = 666print id(a), id(a[0]), id(a[1]), id(a[2])print id(b), id(b[0]), id(b[1]), id(b[2])

It seems correct.

In summary, this is just a series of references. When we modify the modifiable Data Types of the copied objects, the reference is not changed, so the original object will be affected. If you modify an object that cannot be modified, the object is created and the reference is refreshed. Therefore, the result is different from that of the original object.

Method for creating a shortest copy:

1. Slice operation

2. Use the list () factory function to create an object. (B = list ())

Deep copy does not create the referenced objects and generate a new series of references.

This is basically the case, but it seems a waste of memory for re-creation of unmodifiable objects such as strings and numbers. Anyway, you will create new objects when you want to modify them, refresh the referenced. Therefore, it doesn't matter if you still use the original reference. It can also save memory.

Check the code for verification:

from copy import deepcopya = ['scolia', 123, [], ]b = deepcopy(a)b[1] = 666print id(a), id(a[0]), id(a[1]), id(a[2])print id(b), id(b[0]), id(b[1]), id(b[2])

The verification is correct.

Create a deep copy:

1. As in the sample code, you can only use the deepcopy () method of the built-in copy module.

All right, let's talk about the issue of deep copy. If there is any error or something to be supplemented, it will continue.

The above in-depth understanding of the small copy and deep copy in python is all the content shared by the small Editor. I hope to give you a reference and support for the house of friends.

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.