In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to analyze the deep copy and shallow copy of Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
In the usual work, data transmission is often involved, and the problem of data modification may occur in the process of data transmission and use. In order to prevent the data from being modified, it is necessary to transfer a copy, even if the copy is modified, it will not affect the use of the original data. In order to make this copy, a copy is made. Today I will talk about the problem of deep copy and shallow copy in Python.
Conceptual popularity: objects, mutable types, references
Data copy will involve the three concepts of object, variable type and reference in Python. Let's take a look at these concepts first. Only when we understand them can we better understand what deep copy and shallow copy are all about.
Python object
In Python, there is a very popular saying that everything is an object. It is said that any data type constructed is an object, whether it is a number, a string, a function, or even a module. Python treats it as an object.
All Python objects have three properties: identity, type, and value.
Look at a simple example:
In [1]: name = "laowang" # name object In [2]: id (name) # id: unique identity Out [2]: 1698668550104 In [3]: type (name) # type: the type of object that determines what type of value Out [3]: str In [4]: name # object, the data Out [4]: 'laowang'
Mutable and immutable objects
In Python, objects can be divided into two categories according to the way they are updated: mutable objects and immutable objects.
Mutable objects: list, dictionary, collection
The so-called variable means that the value of the variable object is variable and the identity is unchanged.
Immutable objects: numbers, strings, tuples
An immutable object is that the identity and value of the object are immutable. The newly created object is associated with the original variable name, the old object is discarded, and the garbage collector reclaims these objects at the appropriate time.
In [7]: var1 = "python" In [8]: id (var1) Out [8]: 1700782038408 # because var1 is immutable, the java object is recreated, and then the id changes, and the old object python will be recycled at some point in time In [9]: var1 = "java" In [10]: id (var1) Out [10]: 1700767578296
Quote
In the Python program, each object applies for a space in memory to hold the object, and the address of the object's location in memory is called a reference. When developing a program, the variable name defined is actually referenced by the address of the object.
A reference is actually a numeric address number in memory. When using an object, as long as you know the address of the object, you can manipulate the object, but because this numeric address is not convenient to use and remember during development, use the form of the variable name instead of the numeric address of the object. In Python, a variable is a representation of an address and does not open up storage space.
Just like the IP address, when visiting the website, the host is actually determined by the IP address, but the IP address is not easy to remember, so the domain name is used instead of the IP address. When using the domain name to visit the website, the domain name is resolved into an IP address to use.
An example is given to show that variables and references to variables are one thing.
In [11]: age = 18 In [12]: id (age) Out [12]: 1730306752 In [13]: id (18) Out [13]: 1730306752
Step by step: reference assignment
It is clear above that a reference is the numeric address number of an object in memory, that a variable is convenient for the representation of a reference, and that the variable points to this reference. The essence of assignment is to have multiple variables refer to the address of the same object at the same time. So what happens when you modify the data?
The reference assignment of an immutable object.
Assigning values to immutable objects is actually opening up a space in memory to point to new objects, and the original immutable objects will not be modified.
The schematic diagram is as follows:
Let's understand it through a case study:
Both an and b are references to 1 in memory, so the references to an and b are the same.
In [1]: a = 1 In [2]: B = an In [3]: id (a) Out [3]: 1730306496 In [4]: id (b) Out [4]: 1730306496
Now re-assign a to see what happens.
It is not difficult to see from the following: when an is assigned a new object, it will point to the current reference, not to the old object reference.
In [1]: a = 1 In [2]: B = an In [5]: a = 2 In [6]: id (a) Out [6]: 1730306816 In [7]: id (b) Out [7]: 1730306496
A reference assignment to a mutable object.
What a mutable object holds is not the real object data, but the reference to the object. When you assign a value to a mutable object, you simply point the reference saved in the mutable object to the new object.
The schematic diagram is as follows:
Still experience the process of variable object reference assignment through an example.
When you change L1, the reference to the entire list refers to the new object, but both L1 and L2 are references to the same list saved, so the reference address does not change.
In [3]: L1 = [1,2,3] In [4]: L2 = L1 In [5]: id (L1) Out [5]: 1916633584008 In [6]: id (L2) Out [6]: 1916633584008 In [7]: L1 [0] = 11 In [8]: id (L1) Out [8]: 1916633584008 In [9]: id (L2) Out [9]: 1916633584008
Main idea: shallow copy, deep copy
After the interpretation of the first two parts, we should have a clear understanding of the reference assignment of the object.
Let's consider a question like this: how to solve the problem in Python that the original data is not affected after the function is passed?
Python has solved this problem for us, and we can happily solve it by using a copy or deep copy of the object.
Let's take a look at how shallow and deep copies are implemented in Python.
Shallow copy:
In order to solve the problem that the function is modified after transfer, it is necessary to copy a copy and pass the copy to the function, even if the copy is modified, it will not affect the original data.
Copy of immutable object
Immutable objects open up new space in memory only when they are modified, while copying actually allows multiple objects to point to a reference at the same time, which is no different from object assignment.
Similarly, feel it through an example: it is not difficult to see that an and b point to the same reference, and the copy of the immutable object is the object assignment.
In [11]: import copy In [12]: a = 10 In [13]: B = copy.copy (a) In [14]: id (a) Out [14]: 1730306496 In [15]: id (b) Out [15]: 1730306496
Copy of mutable object
For a copy of an immutable object, the reference to the object has not changed, so will the copy of the immutable object be the same as the immutable object? Let's move on.
As can be seen from the following example: a copy of a mutable object opens up a new space in memory to hold the copied data. When you change the previous object, it has no effect on the copied object.
In [24]: import copy In [25]: L1 = [1,2,3] In [26]: L2 = copy.copy (L1) In [27]: id (L1) Out [27]: 1916631742088 In [28]: id (L2) Out [28]: 1916636282952 In [29]: L1 [0] = 11 In [30]: id (L1) Out [30]: 1916631742088 In [31]: id (L2) Out [31]: 1916636282952
The schematic diagram is as follows:
Now let's go back to the question, can shallow copy solve the problem that the original data remains unchanged after the function is passed? Let's look at a slightly more complex data structure.
Through the following example, it can be found that when a complex object is copied, it does not solve the problem of data change after data transfer. The reason for this is that when the copy () function copies an object, it just copies all the references in the specified object, and if these references contain a mutable object, the data will still be changed. This method of copying is called shallow copy.
In [35]: a = [1,2] In [36]: L1 = [3,4, a] In [37]: L2 = copy.copy (L1) In [38]: id (L1) Out [38]: 1916631704520 In [39]: id (L2) Out [39]: 1916631713736 In [40]: a [0] = 11 In [41]: id (L1) Out [41]: 1916631704520 In [42]: id (L2) Out [42]: 1916631713736 In [43]: L1 Out [43]: [3, 4, [11] 2]] In [44]: l2 Out [44]: [3, 4, [11, 2]]
The schematic diagram is as follows:
For the above situation, Python also provides another copy method (deep copy) to solve.
Deep copy
Unlike shallow copies, which only copy top-level references, deep copies are copied layer by layer until all references copied are immutable.
Next, let's see, if the copy instance above is used to use deep copy, will the problem of original data change still exist?
The following example clearly tells us that the previous problem can be solved.
Import copy L1 = [3,4, a] In [47]: L2 = copy.deepcopy (li) In [48]: id (L1) Out [48]: 1916632194312 In [49]: id (L2) Out [49]: 1916634281416 In [50]: a [0] = 11 In [51]: id (L1) Out [51]: 1916632194312 In [52]: 1916634281416 In [54]: L1 Out [54]: [3,4,11] 2]] In [55]: l2 Out [55]: [1, 2, 3]
The schematic diagram is as follows:
Check leaks and fill gaps
Why is the default copy mode of Python shallow copy?
Time Angle: shallow copies take less time.
Space perspective: shallow copies cost less memory.
Efficiency point of view: shallow copy only copies top-level data, which is generally more efficient than deep copy.
Summary of knowledge points:
Immutable objects open up new spaces when assigning values.
When a mutable object is assigned, the value of one is changed, and the other will also change.
Deep and shallow copies do not open up new space when copying immutable objects, which is equivalent to an assignment operation.
Shallow copy when copying, only the references in the * layer are copied. If the element is a mutable object and is modified, the copied object will also change.
Deep copy when copying, it is copied layer by layer until all references are immutable objects.
There are many ways to implement shallow copy in Python, copy function of copy module, copy function of object, factory method, slicing and so on.
In most cases, a shallow copy is used when writing a program, unless there are specific requirements.
The advantages of shallow copy are as follows: fast copying speed, less space occupation and high copying efficiency.
On how to analyze Python deep copy and shallow copy problem is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.