In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the difference between python shallow copy and deep copy". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "what is the difference between python shallow copy and deep copy" together!
Difference between shallow replication and deep replication
Shallow copy and deep copy are mainly the differences that appear in the copy of objects. We know that copy is to obtain one thing exactly the same by certain means. So an object should be copied the same way, but because of the existence of references, there is a certain difference, take a dictionary as an example:
There are two dictionaries:
dict1 = {'Name': 'W3CSchool', 'Age': 7, 'Class': 'First',}dict2 = {'info':dict1,'uid':12444}
We can see that dictionary 1 is referenced in dictionary 2. In fact, dictionary 2 stores the reference address of dictionary 1 instead of specific data. That is to say, when dictionary 1 is modified, dictionary 2 will also change accordingly.
My friends will say, what does this have to do with shallow replication and deep replication? Yes, because if we simply copy dictionary 2, where info copies references to dictionary 1, that is, dictionary 1 is modified, dictionary 2 will be modified, and the copy of dictionary 2 will be modified. If our copy of dictionary 2 is for backup, such backup will not work.
Therefore, there is another kind of copy, which does not copy the reference of dictionary 1, but directly copies the contents of dictionary 1 and adds them to dictionary 2. Therefore, the copy of dictionary 2 is not copying the reference of dictionary 1. When we modify dictionary 1, dictionary 2 will change, but the copy of dictionary 2 will not change.
To distinguish between these two types of copy, we call them shallow copy and deep copy, also called shallow copy and deep copy, their difference lies in the processing of objects in the dictionary, shallow copy only copies references, and deep copy copies the contents of the object into a new copy.
application
In the dictionary backup, using the copy function for backup is a shallow copy.
Deep copying of dictionaries requires the use of the copy module, and after the introduction of the copy module, you can use the deepcopy () method to make deep copies.
import copydict2 = copy.deepcopy(dict1) Thank you for reading, the above is "python shallow copy and deep copy what is the difference" content, after the study of this article, I believe that we have a deeper understanding of python shallow copy and deep copy what is the difference between this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.