This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021
A, the conclusion
A deep-copied object is a completely new object. Both the object itself (ID) and its children are different from the original object.
The object itself (ID) is completely different from the original object, but its children are the same as the children of the original object.
As a side note, the assigned object is exactly the same as the original object, but with a different name.
Use practical examples
Deep copy is copying the object itself (A) to another object (B). This means that changes to the object itself or its children will not affect the original object. In Python, we use the deepcopy() function for deepcopy.
Shallow copy copies a reference to an object to another object. Therefore, if we make a change in a child object, it affects the original object; But making changes to the object itself does not affect the original object. Use the copy() function to make a shallow copy.
Let’s use the actual code to show you (note the comments and output in the code) :
import copy
a = [1.2.3['a'.'b']] # primitive object
b = a # assign, pass a reference to the object
c = copy.copy(a) Object copy, shallow copy
d = copy.deepcopy(a) Object copy, deep copy
a.append(4) Change object A
a[3].append('c') ['a', 'b'];
print('a = ', a)
print('b = ', b)
print('c = ', c)
print('d = ', d)
Copy the code
A is the original object, which contains four elements, three ints, and a list. B is the result of direct (a) assignment, C is the result of shallow copy (a), and D is the result of deep copy (a).
We then modify the list of a by first adding an int (4), then modifying the fourth element of A (list) by adding a new element ‘c’ to the fourth element list, and then printing a, B, C, and D.
From the output result, we can find that b directly assigned to a is exactly the same as A, which synchronously changes all, indicating that the new variable directly assigned to b, whether the object itself, or the child objects contained in the object, are the same and identical.
Object C from the shallow copy only changes the child object to be the same as a. The changes do not take effect, indicating that the shallow copy copies the reference of the object itself to the new object, but contains the same child object as before.
In the end, the deep-copied object is completely unchanged, indicating that the deep-copied object itself and its children are completely new and have nothing to do with the original object.
Next, let’s take a further look. The ids of these objects themselves and the ID of the fourth element in the object can also well confirm the above conclusion.
# A and B are identical objects; A, C and D are different objects
print('id(a) = '.id(a))
print('id(b) = '.id(b))
print('id(c) = '.id(c))
print('id(d) = '.id(d))
Copy the code
# the elements in A and C are the same object and the elements in A and D are not the same object
print('id(a[3]) = '.id(a[3]))
print('id(c[3]) = '.id(c[3]))
print('id(d[3]) = '.id(d[3]))
Copy the code
Three, application scenarios
What are the common references to deep and shallow copies?
From the perspective of data processing, it can be applied in the following scenarios:
1) We usually do not want to modify the original data when processing intermediate results, so we can use deep copy in this case; 2) If we only want to add a secondary column (only involving changes to the parent object), then we can use shallow copy to save system memory.
Shallow copy application
What are the common references to deep and shallow copies?
From the perspective of data processing, it can be applied in the following scenarios:
1) We usually don’t want to make any changes to the original data when processing intermediate results, so we can use deep copy in this case;
2) If we only want to add a secondary column (only involving changes to the parent object), then we can use shallow copy to save system memory.
Hopefully this article has helped you understand deep versus shallow copy.
Welcome to leave a comment in the comments section about the practical application of other deep and light copies
I’m an old cousin. I love cats and technology