In Python, when you assign a value to an object, pass it as a parameter, or return as a result, Python typically uses a reference to the original object (that is, the memory address of the new object is pointed to the original object), not the actual copy. Some of the other languages are copied every time you assign a value. In Python, an "implicit" copy of an assignment operation is not made, and a copy must be explicitly requested.
So, in Python, the object is assigned a shallow copy, which is an object reference by default, while a deep copy is a true copy of the object.
First, let's take a look at the ID function, the official explanation is as follows:
ID (object)
Return the "Identity" of an object. The is a integer (or long Integer) which is guaranteed to being unique and constant for the this object during its lifetime. The same ID () value is objects with non-overlapping lifetimes.
CPython implementation Detail:this is the address of the object in memory.
In short, an ID function is a memory address that returns an object in its own life cycle. Let's look at two examples:
In []: Print ID (' abc ')
3074738896
in [+]: cmd= ' abc '
In []: Print ID (cmd)
3074738896
In [Wuyi]: x= ' BBC '
In [y=]: ' BBC '
In [SI]: Print ID (x), id (y), id (' BBC ')
3052874184 3052874184 3052874184
In Python everything is object, like string abc, a value such as a number is an object, but the number is an integer object, and ' abc ' is a String object. The above assignment operation Cmd= ' abc ', the actual process in Python is this:
First search in memory for the existence of string ABC, if the string ABC already exists in memory, then directly return the existing string ABC memory address, let the variable cmd point to the existing string ABC memory address. If the string ABC does not exist in memory, ask for a memory allocation to a string object to store the string ABC, and then let the variable cmd point to the object, which is actually pointing to the memory (which is a bit like a pointer in the C language). The advantage of this is that it increases speed.
As with the result of ID (' ABC ') and ID (cmd), the description ID function returns the memory address that the object points to when it acts on any object (string object or Variable object).
Two, shallow copy case:
By default, when we insert data into a new object, the old object is also updated (of course, the data is inserted into the original object and the new object is updated):
In [PNS]: a=[1,2,3]
In []: B=a
In [the]: b.append (333)
In [MAX]: Print a
[1, 2, 3, 333]
in [+]: Print b
[1, 2, 3, 333]
In [all]: Print ID (a), id (b)
3054610764 3054610764
Conclusion:
We see through the ID function that two objects, a, a, point to the same piece of memory address space. That is, the assignment operation between their A and B objects is a reference, not a copy. So modify any object, and the other object will change with it.
Three, deep copy case
We can solve the problem of object reference by the following methods:
in [+]: Import copy
In []: a=[1,2,3]
in [+]: b=copy.deepcopy (a)
In [All]: b.append (333)
In [all]: print a
[1, 2, 3]
In []: Print B
[1, 2, 3, 333]
In [review]: Print ID (a), id (b)
3064057292 3064057644
We need to use the Copy.deepcopy method to display the object copy so that there is no problem with the previous copy.
Four, determine whether two objects are the same
1, you can use the ID function to determine
2, use the following method
In []: a=[1,2,3]
In [B=a]:
In [the]: A is b
OUT[74]: True
In []: a=[1,2,3]
In []: B=copy.deepcopy (a)
in [+]: A is b
OUT[77]: False
This article is from the "Good" blog, please be sure to keep this source http://leejia.blog.51cto.com/4356849/1589466
Python deep copy and shallow copy