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

Source: Internet
Author: User
Tags shallow copy in python

Like many languages, Python is divided into "copy" Ways of simple assignments, shallow copies, and deep copies.

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

One, assign the value

Assignment is the most common of these three operations, and we analyze the assignment operation by some examples:

STR cases

Copy Code code as follows:

>>> a = ' Hello '
>>> b = ' Hello '
>>> C = A
>>> [ID (x) for x in A,b,c]
[4404120000, 4404120000, 4404120000]

From the above instructions, we can find that the addresses of a, B, and C are the same. So the operation of the above assignment is equivalent to c = a = b = ' Hello '.

The assignment is that the system assigns memory to a variable or object (here is ' hello '), and then assigns the address to A, B, and C. So their addresses are the same.

List example

Copy Code code as follows:

>>> a = [' Hello ']
>>> b = [' Hello ']
>>> C = A
>>> [ID (x) for x in A,b,c]
[4403975952, 4404095096, 4403975952]

But this is not the case, the address of A and B is different. Why?

Because STR is immutable, the same ' hello ' has only one address, but the list is variable, so two addresses must be assigned.

At this point, we would like to explore the above two cases, if the value of the modified?

STR cases

Copy Code code as follows:

>>> a = ' world '
>>> [ID (x) for x in A,b,c]
[4404120432, 4404120000, 4404120000]
>>> print A, B, c
World Hello Hello

At this point A's address and value have changed, but B, c address and values have not changed. Because of the immutability of STR, A is going to have to recreate the memory space, so the value of a is changed and the address that a points to changes. b, c because of the invariance of ' hello ', will not change.

List example

Copy Code code as follows:

>>> a[0] = ' world '
>>> [ID (x) for x in A,b,c]
[4403975952, 4404095096, 4403975952]
>>> print A, B, c
[' World '] [' Hello '] [' World ']

At this point A, C and the value and address are changed, but the two are still the same, B does not change. Because of the variability of the list, changing the value of the list does not require additional space, just modify the value of the original address. So a, c all change.

Having understood the above differences, we are able to analyze the shallow and deep copies well.

We all use list as an example.

Second, shallow copy

Copy Code code as follows:

>>> a = [' Hello ', [123, 234]]
>>> B = a[:]
>>> [ID (x) for x in A,b]
[4496003656, 4496066752]
>>> [ID (x) for x in a]
[4496091584, 4495947536]
>>> [ID (x) for x in B]
[4496091584, 4495947536]

line3,4 can see that a, B address is different, which conforms to the list is variable, should open up different space. Is that a copy of the light copy? Looking at line5-8, we find that the address of the element in A, B is the same. If the string ' Hello ' address is consistent and understandable, the second element is still consistent with the list address. This illustrates the characteristics of a shallow copy, which simply copies the address of the element within the container.

Then we try to modify the values in A, B:

Copy Code code 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 in a is changed by STR, but not in B, and the second element in a is changed in B. This conforms to immutable object modification will open up new space, variable object modification will not open up new space. It is further proved that a shallow copy is simply a copy of the address of the element in the container.

Third, deep copy

Copy Code code as follows:

>>> from copy import deepcopy
>>> a = [' Hello ', [123, 234]]
>>> B = Deepcopy (a)
>>> [ID (x) for x in a, b]
[4496066824, 4496066680]
>>> [ID (x) for x in a]
[4496091584, 4496067040]
>>> [ID (x) for x in B]
[4496091584, 4496371792]

After a deep copy, you can find a, B address, and the element addresses in a, B are different. This is the complete copy of a copy.

After modifying the value of a:

Copy Code code 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 the line4,5 can be found that only a modified, b no changes. Because B is a full copy, the element address is different from a, a modification, B is not affected.

Summarize:

1. Assignment is to assign the address of an object to a variable, and let the variable point to the address (old bottle old wine).

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

3. A deep copy creates a new variable or container in another address, and the address of the element within the container is also newly opened, just the same value, and is a full copy. In other words (new bottles of new wine).

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.