In Python, an object assignment is actually a reference to an object. When you create an object and then assign it to another variable, Python does not copy the object, but simply copies the object's reference. Here are two ideas for understanding shallow and deep copies, respectively:
- Using the slice operation and factory method list method copy
- Copy using the Deepcopy method in copy
1. Using the slicing operation and the Factory method list method copy
Code scenario: There is a guy Jack,tom copy jack through a slicing operation Jack,anny the factory method.
>>> jack = [' Jack ', [' Age ', 20]]>>> tom = jack[:]>>> Anny = List (Jack)
Look at the ID values for the three:
>>> Print ID (jack), ID (TOM), ID (anny) 144846988 144977164 144977388
Judging from the ID value, the three are different objects. Rename Tom and Anny to their respective names:
>>> tom[0] = ' Tom ' >>> anny[0] = ' Anny ' >>> print Jack, Tom, anny[' Jack ', [' age ', ' [']] [' Tom ', [' Age ', "] [' Anny ', [' age ', 20]]
From here to see everything is normal, but Anny only 18 years old, Anny defined age.
>>> anny[1][1] = 18>>> print jack, Tom, anny[' Jack ', [' age ', [+]] [' Tom ', [' age ', ' +]] [' Anny ', [' age ', 18]]
At this time, strange things happened, Jack, Tom, Anny's age has changed, has become 18. Jack, Tom, Anny they should all be different objects, how can they affect each other? Look at the inner elements of Jack,tom,anny each element ID:
>>> [ID (x) for x in jack][3073896320l, 3073777580l]>>> [ID (x) for x in tom][144870744, 3073777580l]>& gt;> [ID (x) for x in anny][144977344, 3073777580L]
It dawned on Jack, Tom and Anny that the elements of the age element point to the same element. Modify one of them, of course, affect other people. Then why does the change of name not affect it? It turns out that strings in Python cannot be modified, so when you rename Tom and Anny, a ' Tom ' and ' Anny ' object is recreated, replacing the old ' Jack ' object. For the sake of understanding, I drew a sketch:
Python Shallow copy plot
2. Copy using the Deepcopy method in copy
In order to keep them from interacting with each other, try Deepcopy.
>>> jack = [' Jack ', [' age ', ']]>>> ' import copy>>> tom = Copy.deepcopy (Jack) >>> Ann y = copy.deepcopy (Jack)
Rename according to the first idea, and reset the age operation:
>>> tom[0] = ' Tom ' >>> anny[0] = ' Anny ' >>> print Jack, Tom, anny[' Jack ', [' age ', ' [']] [' Tom ', [' Age ', ' 20 '] [' Anny ', [' age ', ']]>>> anny[1][1] = 18>>> print jack, Tom, anny[' Jack ', [' age ', ' [']] [' Tom ', [' Age ', ' [']] [' Anny ', [' age ', 18]]
At this time they will not affect each other. Print out each person's internal element each ID:
>>> [ID (x) for x in jack][139132064, 3073507244l]>>> [ID (x) for x in tom][139137464, 139132204]>>& Gt [ID (x) for x in Anny] [139141632, 139157548]
Their internal elements also point to different objects.
Summary:
Idea one: Using slicing operations and factory methods The list method copy is called a shallow copy, just copy the outermost object itself, the internal elements are just a copy of a reference.
Idea two: Copying using the Deepcopy method in copy is called a deep copy, and the outer and inner elements copy the object itself, not the reference.
But for numbers, strings, and other atomic type objects, there is no copy of the argument, even with a deep copy, the same is true if you look at the ID, and if you re-assign it, just create a new object and replace the old one.
Deep copy and shallow copy understanding in Python