Python shallow copy vs deep copy

Literally understood, copy is to copy the current object. But only container objects (lists, tuples, dictionaries) have the concept of copying, and atomic objects (numbers, strings) do not have the concept of copying .


introduce


Ordinary copies are shallow copies.
Shallow copy can be understood that although the B container generated by the copy of the A container is new, the content in the B container is still old. It simply copies the reference of the content in the A container, and does not create it in the B container. real object. When we modify the contents of the B container, there will be two results: the immutable object will build a new object, and its id will change; and the mutable object will be modified according to the reference, and its change will affect the A container. The corresponding part has also changed.
In order to make the modification of the mutable objects in the container not affect each other and get a true copy, the concept of deep copy is introduced, which is also called full copy.

Note:
id can simply be regarded as an address (in fact, it is not), and the "id(obj)" function can print out the id of the obj object.
Mutable objects refer to their contents that can be changed, specifically lists and dictionaries; immutable objects are the opposite, specifically numbers, strings and tuples.


Combine code:
#!/usr/bin/env python
# coding=utf-8

from copy import copy, deepcopy


def id_print(old, new):
    print 'old object: ', old
    print 'new object: ', new
    print 'id of old object and new object: %d, %d' % (id(old), id(new))
    print 'old[0] and new[0] id: %d, %d' % (id(old[0]), id(new[0]))
    print 'old[1] and new[1] id: %d, %d' % (id(old[1]), id(new[1]))


old = ['old', ['old', 1]]

# new = old[:] # Slice, produce shallow copy
# new = list(old) # Factory function for list, producing shallow copy
# new = copy(old) # The copy function of the copy module produces a shallow copy
# new = deepcopy(old) # The deepcopy function of the copy module generates a deep copy

print '*' * 20 + 'before modification' + '*' * 20
id_print(old, new)

new[0] = 'new' # modify immutable object
new[1][0] = 'new' # Modify the contents of the mutable object

print '*' * 20 + 'modified' + '*' * 20
id_print(old, new)
The running results of shallow copy and deep copy will be presented separately below.


shallow copy


After uncommenting the corresponding comments, the running results:


1) Through the slice operation, the factory functions (list(), tuple(), dict()) or the copy function of the copy module generate shallow copies.
2) The ids of the two objects obtained by the shallow copy are different, but the ids of the content are the same. If the content changes, the ids of immutable objects will be different and will not affect each other; the ids of mutable objects will remain the same and affect each other.



deep copy


After uncommenting the corresponding comment, the running result:


1) Using the deepcopy function of the copy module produces a deep copy.
2) The ids of the two objects obtained by deep copying are different, the ids of the mutable objects in the content are different, and the ids of the immutable objects in the content are the same. If the content changes, the ids of the immutable objects become different and do not affect each other; the ids of the mutable objects are still different and do not affect each other.



Refer to Section 6.20 of Python Core Programming Second Edition; if there is any inappropriate place in the text, please include and point out, thank you


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325441442&siteId=291194637