Shallow copy and deep copy in python

高洛峰
Release: 2017-03-02 15:56:04
Original
1329 people have browsed it

Before we talk about what deep and shallow copies are, let’s first look at this phenomenon:

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

Shallow copy and deep copy in python

Why do I only modify b, but it affects a? I have seen what I said in my previous article: all the memory references are stored in the sequence.

Shallow copy and deep copy in python

So, when we modify the empty list inside through b, we are actually modifying the same object in the memory, so it will affect a.

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])
Copy after login

Shallow copy and deep copy in python

The code is verified correctly, so although a and b are two different objects, the references inside are both the same. This is the so-called new object, old content.

However, shallow copy is not just that, look below:

a = ['scolia', 123, [], ]
b = a[:]
b[1] = 666
print a
print b
Copy after login

Shallow copy and deep copy in python

What is going on here? What's going on?

Students who have read my instructions on variable assignment in Python will know: For immutable data types such as strings and numbers, modification is equivalent to reassignment. Here it is equivalent to refreshing the reference.

Shallow copy and deep copy in python

Verify the code:

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

Shallow copy and deep copy in python

It seems that it is correct.

The above mentioned are shallow copies. To sum up, shallow copies only copy a series of references. When we modify the modifiable data type of the copied object, the references are not changed, so Will affect the original object. When modifying an unmodifiable object, a new object is created and the reference is refreshed. Therefore, the reference to the original object is different, and the result is different.

Method to create a shallow copy:

1. Slicing operation

2. Use the list() factory function to create a new object . (b = list(a))

So a deep copy means re-creating the objects referenced inside and generating a new series of references.

Basically, it is like this, but for unmodifiable objects such as strings and numbers, it seems a bit wasteful to create a new copy. Anyway, when you want to modify it, you will create a new object and refresh the reference. of. So it doesn't matter if you still use the original reference, it can also achieve the purpose of saving memory.

Shallow copy and deep copy in python

Look at the code verification:

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

Shallow copy and deep copy in python

Verified correctly .

Creation of deep copy:

1. Just like the code example, it can only be created through the deepcopy() method of the built-in copy module .

Okay, let’s stop here for the issue of deep and shallow copying. If there are any mistakes or need to be added, we will continue later.

The above article provides an in-depth understanding of shallow copy and deep copy in python. This is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles related to shallow copy and deep copy in python, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template