When summarizing Python objects and references, it is also possible to understand the deep and shallow copies of Python in fact.
In Python, an object's assignment is actually a reference to the object. That is, when you create an object and then assign it to another variable, you are actually copying the reference to the object.
We'll start by using the slice operation and the Factory method list method to illustrate the shallow copy.
Give me a chestnut:
Tom = [' Tom ', [' age ', 10]]jack = tom[:] ... Slicing Operations June = List (Tom)
Next, take a look at the references to the three variables above:
>>> ID (TOM) 140466700697824>>> ID (Jack) 140466700700488>>> ID (June) 140466700770192
It can be found that three variables point to different objects respectively. Let's take a look at the contents of these three objects:
>>> tom[' Tom ', [' age ', 10]]>>> jack[' Tom ', [' age ', 10]]>>> june[' Tom ', [' age ', 10]
It is clear that the contents of these three objects will be the same, since the objects referenced by Tom are copied through the above slicing operation and the Factory function list. The next step is further validation:
We modify the objects that Jack and June refer to:
>>> jack[0] = ' Jack ' >>> jack[' Jack ', [' age ', 10]]>>> june[0] = ' June ' >>> june[' June ' , [' Age ', 10]]
Now we're going to make changes to Jack's age:
>>> jack[1][1] = 20>>> jack[' Jack ', [' age ', 20]]
You can see that Jack's age has changed to 20; let's look at Tom, June's Age:
>>> print Tom, Jack, june[' Tom ', [' age ', [+]] [' Jack ', [' age ', ' [']] [' June ', [' age ', 20]]
The strange thing happened, we just modified Jack's age, but Tom and June's age followed change, this is why?
This involves the mystery of the shallow copy in Python:
Let's take a look at the memory address of the inner element of Tom, Jack, June:
>>> for x in Tom: ... print ID (x) ... 140704715293600-- ' Tom ' 140704715147816- -[' age ', 20]>>>-X in Jack: ... print ID (x) ... 140704715286256--' Jack ' 140704715147816--[' age ', 20]>>>-X in June: ... print ID (x) ... 140704715286352--' June ' 140704715147816-->[' age ', 20]
Look closely, Tom, Jack, June. The age element of three variables [' ages ', 20] points to the same object; Why do they have their name elements pointing to different objects, respectively? This is because, in Python, it is divided into mutable data Objects (lists, dictionaries) and immutable Data Objects (integer, String, floating-point, Ganso). It is for this reason that their name elements are strings and immutable data objects, so when Jack and June are renamed, the Jack and June objects are actually created in memory. The age element is a mutable data object, so the new object is not created in memory, and the age element of Tom,jack,june refers to the same object, resulting in the modification of one of them to change the ages of the other two.
This is a shallow copy of Python, which simply copies a whole object (which is supposed to be the outermost layer of an object) and does not copy the elements contained within the object.
Next, we use the Deepcopy method in copy to illustrate the deep copy:
Or with the chestnuts above:
To make Tom, Jack, June do not affect each other, we use the Deepcopy method to copy Tom to build Jack and June:
>>> Tom = [' Tom ', [' Age ', 10]]>>> import copy>>> Jack = copy.deepcopy (tom) >>> June = C Opy.deepcopy (Tom) >>> jack[' Tom ', [' age ', 10]]>>> june[' Tom ', [' age ', 10]]>>> tom[' Tom ', [' Age ', 10]]
Let's take a look at the memory address that Tom, Jack, June points to respectively:
>>> Print ID (Tom), id (June), id (Jack) 140707738759392 140707738799280 140707738797192
Three memory addresses are different, then we then change the names of Jack and June, and look at the memory addresses that the internal elements of the changes point to:
>>> jack[0] = ' Jack ' >>> june[0] = ' June ' >>> tom[' Tom ', [' age ', 10]]>>> jack[' Jack ', [ ' Age ', 10]]>>> june[' June ', [' age ', 10]]>>> for x in Tom: ... print ID (x) ... 140707738882976-- ' Tom ' 140707738737192- -[' age ', 10]>>>-X in Jack: ... print ID (x) ... 140707738875584-- ' Jack ' 140707738910016- -[' age ', 10]>>>-X in June: ... print ID (x) ... 140707738876640 --' June ' 140707738910160 --[' age ', 10]
It is clear that their internal elements also point to different objects, stating that the elements are completely copied (including internal elements) through the Deepcopy method.
Finally summarize the idea:
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.
Understanding of deep and shallow copies in Python