Introduction to assignment, shallow copy, and deep copy in Python

Source: Internet
Author: User
This article mainly introduces the assignment, shortest, and deep copy methods in Python. Python also includes simple assignment, shortest, and deep copy methods, for more information about how to copy data, see the following methods in Python: Simple assignment, shallow copy, and deep copy.

In the learning process, the initial understanding of the shallow copy is vague. However, after a series of experiments, I found that I had a better understanding of the three concepts.

I. Assignment

Assignment is the most common of the three operations. We use some examples to analyze the assignment operation:

Str example

The code is as follows:


>>> A = 'Hello'
>>> B = 'Hello'
>>> C =
>>> [Id (x) for x in a, B, c]
[4404120000,440, 4404120000]


From the preceding instructions, we can find that the addresses a, B, and c are the same. Therefore, the assignment operation is equivalent to c = a = B = 'hello '.

The system first allocates memory to a variable or object ('Hello' here), and then assigns the address to a, B, and c. So their addresses are the same.

List example

The code is as follows:


>>> A = ['hello']
>>> B = ['hello']
>>> C =
>>> [Id (x) for x in a, B, c]
[4403975952,440 4095096, 4403975952]


But this is different. the addresses of a and B are different. Why?

Because str is immutable, 'Hello' has only one address, but the list is variable. Therefore, two addresses must be allocated.

At this time, we want to explore the above two situations, what if we modify the value?

Str example

The code is as follows:


>>> A = 'world'
>>> [Id (x) for x in a, B, c]
[4404120432,440, 4404120000]
>>> Print a, B, c
World hello


At this time, the address and value of a have changed, but the address and value of B and c have not changed. Because 'str' is immutable, to assign a value again, you need to re-open the memory space. Therefore, the value of 'A' is changed and the address pointed to by 'A' is changed. B, c will not change because of the immutability of 'hello.

List example

The code is as follows:


>>> A [0] = 'world'
>>> [Id (x) for x in a, B, c]
[4403975952,440 4095096, 4403975952]
>>> Print a, B, c
['World'] ['hello'] ['world']


At this time, the value and address of a and c are changed, but the two are still the same, and B is not changed. Because of the variability of the list, you do not need to open up space to modify the value of the list. you only need to modify the value of the original address. Therefore, both a and c are changed.

After understanding the differences above, we can analyze the shortest copy and deep copy.

We use list as an example.

II. shallow copy

The code is as follows:


>>> A = ['hello', [123,234]
>>> B = a [:]
>>> [Id (x) for x in a, B]
[4496003656,449 6066752]
>>> [Id (x) for x in a]
[4496091584,449 5947536]
>>> [Id (x) for x in B]
[4496091584,449 5947536]


Line3, 4 we can see that the address a and address B are different, which conforms to the variable list and should open up different spaces. Is there a copy for the shortest copy? Let's look at Line5-8. we find that the element addresses in a and B are the same. If the string 'hello' is consistent, it can be understood, but the second element is that the list address is consistent. This illustrates the features of the shortest copy, but copies the addresses of elements in the container.

Next we try to modify the values in a and B:

The code is as follows:


>>> A [0] = 'world'
>>> A [1]. append (345)
>>> Print 'a = ', a,' \ n \ r', 'B =', B
A = ['world', [123,234,345]
B = ['hello', [123,234,345]


The first element 'str' in 'a' changes, but 'B' does not. The second element in 'a' changes, and 'B' also changes. In this way, immutable object modifications will open up new spaces, and immutable object modifications will not open up new spaces. It is further proved that the light copy only copies the addresses of elements in the container.

III. deep copy

The code is as follows:


>>> From copy import deepcopy
>>> A = ['hello', [123,234]
>>> B = deepcopy ()
>>> [Id (x) for x in a, B]
[4496066824,449 6066680]
>>> [Id (x) for x in a]
[4496091584,449 6067040]
>>> [Id (x) for x in B]
[4496091584,449 6371792]


After deep copy, we can find that the element addresses in a, B and a and B are different. This means that a copy is completely copied.

After modifying the value of:

The code is as follows:


>>> A [0] = 'world'
>>> A [1]. append (345)
>>> Print 'a = ', a,' \ n \ r', 'B =', B
A = ['world', [123,234,345]
B = ['hello', [123,234]


From Line4 and 5, we can find that only a is modified and B is not modified. Because B is a complete copy, the element address is different from a, and a is modified, B is not affected.

Summary:

1. assign a value to assign the address of an object to a variable to point the variable to the address (old bottled old wine ).

2. a shallow copy creates a new variable or container in another address, but the address of the element in the container is the copy of the address of the element of the source object. That is to say, the new container points to the old elements (new bottles and old wines ).

3. the deep copy operation creates a new variable or container in another address, and the address of the element in the container is also newly opened, which is only the same value and a complete copy. That is to say (new bottled new wine ).

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.