Shallow and deep copies
Copy functions are specialized for variable data types.list、set、dictA function that is used. The effect is that when a value points to another value, it also does not affect the value pointed to, and if the data being pointed to is mutable, then once it is modified, the data pointed to will also change.
What is variable and non-variable data
Let's take an example, integers are immutable data, so why are they immutable? Whether a data is mutable or not has to do with python's caching mechanism.
When a piece of data changes, if its memory address does not change, it is a mutable piece of data.
For example, let's now create a variable with a value of a, which has a value of 100, and then let that value change, observing whether the memory address of the individual variable has changed.
a = 100 print(a, id(a)) # 100 1610845392 a += 100 print(a, id(a)) # 200 1610848592
We find that the value has changed and the variable's memory has changed along with it, so let's create another variable, b, with the value also integer 100
b = 100 print(b, id(b)) # 100 1610845392
The memory address of b is found to be the same as the memory address of a. That is to say, for a data type like integer, a single number occupies a single memory address, and when a variable pointing to this value changes, it is not the value of the variable that has to change, but rather, it is the variable that has to look for the memory address of the changed value and point to it again. As long as your hardware is not rebooted, that memory address will never change, and such data is immutable.
Then, the opposite is variable data, which refers to a value that actually changes at this memory address after the value pointed to by the variable changes, is a variable data type.
For example, the list, after the list is changed, it is changed on the basis of the original, so the memory address is not changed, this is the variable data type, variable data types do not have memory caching mechanism, can not save memory, so exactly the same data, their memory address may be different.
a = [1, 2] print(a, id(a)) # [1, 2] 1528536069896 (3) print(a, id(a)) # [1, 2, 3] 1528536069896 # b and a have the same value but different memory addresses b = [1, 2, 3] print(b, id(b)) # [1, 2, 3] 1528536069832
So what does the copy function do?
In our practical work, we often use a kind of operation is to define a variable, its value is directly assigned to an original variable. However, after the definition of a variable, we never use it as a decoration, but to do arithmetic, or to do a temporary storage, then the value of the original variable is to be changed, the problem comes, if it is an immutable data is okay, if it is variable data, the direct assignment of their memory address is the same, if the value of a variable changes, the value of the same memory address will change, our value to the temporary storage is also no longer the value we want. If the value of a variable changes, all the values in the same memory address will change, and the value we want to store temporarily will no longer be the value we want, which is a result we don't want to see most of the time.
Let's take an integer as an example, variable a is directly assigned to variable b. The values of variables a and b are the same at this time, but if the value of variable a changes, it does not affect the value of variable b in any way.
a = 100 print(a, id(a)) # 100 1610845392 b = a print(b, id(b)) # 100 1610845392 a += 100 print(a, id(a)) # 200 1610848592 print(b, id(b)) # 100 1610845392
But that's not the case if it's variable data
a = [1, 2] print(a, id(a)) # [1, 2] 2077688035080 b = a print(b, id(b)) # [1, 2] 2077688035080 (3) print(a, id(a)) # [1, 2, 3] 2077688035080 print(b, id(b)) # [1, 2, 3] 2077688035080
This characteristic of immutable data is both an advantage and a disadvantage, the disadvantage is that if we want to save a variable before the change of a state of affairs, it is not saved, this time there is a copy function, which can be variable data into immutable data like the effect.
shallow copy
Using the copy function, the variable a is placed as an argument to the function, and the variable b is used to accept the return value of the function, thus successfully copying the variable a. The memory address of the variable b is not the same as that of the variable a, so that when one of them changes, it will not affect the data of the other.
# The copy function cannot be used directly, you need to import the copy module using import, the copy function of the copy module is a shallow copy. import copy a = [1, 2, 3] # Instead of being a direct assignment of variable a, variable b is returned by the copy function. b = (a) # They have the same value, but different memory addresses, so a change in either of them won't affect the second. print(a, id(a)) # [1, 2, 3] 2343743813320 print(b, id(b)) # [1, 2, 3] 2343743813192 (4) print(a, id(a)) # [1, 2, 3, 4] 2343743813320 print(b, id(b)) # [1, 2, 3] 2343743813192
But if variable a is a second-level container or a more-level container, shallow copy can't copy the second-level container or more-level container, so when the second-level container or more-level container changes, it will still change, because shallow copy can only copy the first-level container, so the memory address of the multi-level container is still the same.
import copy a = [[66,88], 2, 3] b = (a) print(a, id(a)) # [[66, 88], 2, 3] 2431683163720 print(b, id(b)) # [[66, 88], 2, 3] 2431683162184 # Change secondary containers a[0].append(100) print(a, id(a)) # [[66, 88, 100], 2, 3] 2431683163720 print(b, id(b)) # [[66, 88, 100], 2, 3] 2431683162184 # Shallow copy can't copy containers of two levels or more print(id(a[0])) # 1582481372872 print(id(b[0])) # 1582481372872
deep copy
Shallow copies can only copy one level of the container
That's why deep copy was born, which copies all levels of containers.
import copy a = [[66,88], 2, 3] # Deep copy using the deepcopy function b = (a) print(a, id(a)) # [[66, 88], 2, 3] 2168411158088 print(b, id(b)) # [[66, 88], 2, 3] 2168411156552 a[0].append(100) print(a, id(a)) # [[66, 88, 100], 2, 3] 2168411158088 print(b, id(b)) # [[66, 88], 2, 3] 2168411156552 # Deep copy all levels of containers print(id(a[0])) # 2168411158216 print(id(b[0])) # 2168411122760
summarize
To use deep and shallow copy you need to import the copy module;
Shallow copy uses the copy function, which can only copy all elements of a one-level container;
Deep copy uses the deepcopy function, which copies all elements of a container at all levels;
standard librarycopy
Onlycopy
cap (a poem)deepcopy
Two functions are open for public use;
Because deep copy to copy more elements, so the speed will be far less than shallow copy, in the process of programming should be careful to avoid causing excess system burden;
The immutable data in python is Number, string, tuple, and the mutable data is list, set, dict; and copy is specially provided for mutable data, so deep and shallow copy only applies to list, set, dict, of course, mutable data can't go wrong by using the copy function, but it doesn't make sense.
This article on Python mutable and immutable data and deep copy and shallow copy of the article is introduced to this, more related Python data and copy content, please search my previous posts or continue to browse the following related articles I hope that you will support me in the future more!