The difference between a deep copy and a shallow copy in Python:Original April 20, 2017 16:58:35
- Label:
- Python/
- Two copies of Python/
- Deep Copy Light copy/
- Copy Difference
Defined:
The assignment of an object in Python is actually a reference to the object. When creating an object and assigning it to another variable, Python does not copy the object, only the reference to the object is copied.
Shallow copy: Copies the outermost object itself, and the internal elements are just copies of a reference. That is, copying the object again, but other objects referenced in the object I do not copy
Deep copy: Both the outer and inner elements are copied to the object itself, not the reference. That is, copy the object again, and I copy the other objects referenced in the object.
Explanation of several terms:
1, variable: is an element of a system table and has a connection space to the object
2, object: The allocated piece of memory that stores the value it represents
3, reference: is an automatically formed pointer from a variable to an object
4, Note: type (int type, long type (Python3 has removed long, only data of type int)) belongs to object, not variable
5, immutable objects: objects that cannot be modified as soon as they are created, including strings, tuples, numbers
6, mutable objects: objects that can be modified, including lists, dictionaries.
Scope of application:
1, slices can be applied to: lists, tuples, strings, but not to dictionaries.
2, deep copy, can apply a sequence (list, tuple, string), or a dictionary.
The role of the shades of copy:
1, reduce the use of memory
2, in the future to do the data cleaning, modification or storage, the original data copied a copy, in case the data changes, the original data can not be found.
For a dark copy of an immutable object:
Immutable object types, not copied, even with a deep copy, look at the ID is the same, if it is re-assigned, it is only new to create an object, replace the old.
In a word, an immutable type, whether deep or shallow, is the same as the value of the address and the copy.
A=(1, 2, 3)Print"===== First type = shallow copy =====") b=aPrint (a)Print (b)Print (ID (a))Print (ID (b))Print"===== another copy shallow copy = = =") b=Copy.Copy (a)Print (a)Print (b)Print (ID (a))Print (ID (b))Print"===== deep copy =====") b=Copy.deepcopy (a)Print (a)Print (b)Print (ID (a))Print (ID (b)) # The results are as follows: ===== Shallow copy ===== (1, 2, 3) (1, 2, 3) 28145223359522814522335952===== another shallow copy = = = (1, 2, 3) (1, 2, 3) 28145223359522814522335952===== deep copy ===== (1, 2, 3) (1, 2, 3) 28145223359522814522335952
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
For variable Object shade copies:
= Shallow copy: Equal value, equal address
Copy shallow copy: value equal, address not equal
Deepcopy deep copy: Values are equal, address is not equal
A=[1, 2, 3]Print"===== First type = shallow copy =====") b=aPrint (a)Print (b)Print (ID (a))Print (ID (b))Print"===== another copy shallow copy = = =") b=Copy.Copy (a)Print (a)Print (b)Print (ID (a))Print (ID (b))Print"===== deep copy =====") b=Copy.deepcopy (a)Print (a)Print (b)Print (ID (a))Print (ID (b)) #结果如下: ===== Shallow copy =====[1, 2, 3] [1, 2, 3] 20076963215442007696321544===== Another copy shallow copy ===[1, 2, 3][1, 2, 3]20076963215442007695909960===== deep copy =====[1,< Span class= "Hljs-number" > 2, 3][1, 2 , 3]20076963215442007696319560
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
Summarize:
1, a depth copy is a copy of the source object, occupying different memory space.
2, an immutable type of object that has no effect on a deep copy, and the final address value and value are equal.
3, Variable type:
= Shallow copy: Equal value, equal address
Copy shallow copy: value equal, address not equal
Deepcopy deep copy: Values are equal, address is not equal
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. 70271868
The difference between a deep copy and a shallow copy in Python