How to realize Deep and shallow copy with Python
This article mainly introduces how to achieve deep and shallow copy of Python, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Deep and shallow copy
Object reference, shallow copy, deep copy (expansion, difficulty, focus)
In Python, there is a difference between object assignment and copy (deep / shallow copy). If you don't pay attention to it when using it, unexpected results may occur.
In fact, this is the result of shared memory.
Copy: in principle, it is to separate the data, copy it, and modify it later without affecting each other.
Let's first look at a non-copy example.
Use = assignment (object reference)
= assignment: data is fully shared
= assignment is to point to the same object in memory. If it is a mutable type, such as a list, modify one of them, the other must change.
If it is an immutable type (immutable), such as a string, modify one of them, the other will not change
Shallow copy (copy)
Shallow copy: data semi-sharing (copy its data in independent memory, but only copy the first layer successfully)
The above is all the contents of the article "how to make a deep and shallow copy of Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!