To get a clear picture of a deep copy in Python, you need to figure out the following concepts:
Variables-References-Objects (Mutable objects, immutable objects)-slices-copy (shallow copy, deep copy)
" Variable-Object-Reference "
In Python everything is an object, such as:3, 3.14, ' Hello ', [1,2,3,4],{' a ': 1} ...
Even the type itself is an object, type Object
The variables in Python are different from C/c++/java, which refers to the object's reference, Python is a dynamic type, and when the program runs, it is based on the type of the object
To determine what type of variable it is.
assign a value individually : Say, for example:
>>> A = 3
After running a=3, variable a becomes a reference to object 3. Internally, the variable is actually a pointer to the object memory space
Because a python variable is simply a reference to an object, or to the ruling of an object, variable references can often be changed in a program
>>> x = #变量绑定到整型对象
>>> x = ' Hello ' #现在又成了字符串
>>> x = [#现在又成了列表]
The professional statement is as follows:
A variable is an element of a system table that has a connection to an object
Object is a piece of memory that is allocated, storing the value it represents
A reference is an automatically formed pointer from a variable to an object
Special Note: types belong to objects, not variables
For example, like the A=3, the integer object 3 contains the dual information
1. value is 3
2. A header message: Tell Pthyon that this is an integer object [equivalent to a pointer to int]
shared references : for example,
>>> A = 3
>>> B = A
After you run assignment statement B = A, the variable A and variable B point to the memory space of the same object.
As you can see, A and B, whose IDs are exactly the same, point to the same integer object 3, or the same piece of memory.
If you delete a, it will not affect B
The introduction of copy concepts is based on the potential side effects of shared references to mutable objects.
" mutable objects- immutable Objects "
Immutable objects in Python refer to objects that cannot be modified once created, including strings, tuples, numbers
mutable objects in Python refer to: Objects that can be modified, including: lists, dictionaries
The above said a, b are integers, integers are immutable objects, if they are mutable objects, it is another matter.
- >>> L1 = [2,3,4] #L1变量指向的是一个可变对象: List
- >>> L2 = L1 #将L1值赋给L2后, both share references to the same list object [1,2,3,4]
- >>> l1[0] = #因为列表可变, change the value of the first element in the L1
- >>> L1; L2 #改变后, l1,l2 change at the same time, because the value of the object itself has changed.
- [3, 4]
- [3, 4]
If you do not want to change the value of the list L2, there are two ways : slice and copy module
- >>> L1 = [2,3,4]
- >>> L2 = L1
- >>> ID (L1); ID (L2) #共享引用一个可变对象
- 45811784L
- 45811784L
- >>> L2 = l1[:] #切片操作
- >>> ID (L1); ID (L2) #切片后, the object is different.
- 45811784L
- 45806920L
- >>> l1[0] =
- >>> L1; L2 #L1发生改变, L2 no change
- [3, 4]
- [2, 3, 4]
" copy "
1. slicing techniques are applied to all sequences, including: List, String, Ganso
>>> but slices cannot be applied to dictionaries. You can only use the d.copy () method or the d.deepcopy () method for a dictionary.
2. deep Copy, can be used for sequence, also can be used in dictionary
>>> Import Copy
>>> X = copy.copy (Y) #浅拷贝: Copies only top-level objects, or: parent objects
>>> X = copy.deepcopy (Y) #深拷贝: Copies all objects, top-level objects, and their nested objects. Or, the parent object and its child objects
- if the dictionary has only top-level objects :
- If the object is nested in the dictionary:
" Conclusion"
- A deep copy is a copy of the source object, taking up different memory space
- If the source object has only one level directory, the source makes any changes, does not affect the dark Copy object
- If the source object has more than one level of directory, the source makes any changes to affect the shallow copy, but does not affect the deep copy
- The slices of a sequence object are actually shallow copies, that is, only top-level objects are copied
A detailed copy of the depth in Python