Deep copy and shallow copy in Python 3

Keywords: Programming Python

Detailed explanation of deep copy and shallow copy in python

Summary

In python's syntax, there are two ways to copy variables One is deep copy, the other is shallow copy

Let's start with the deep copy

grammar

Here, you need to import the deep copy in the copy module of the system import copy New object = copy.deep copy (copied object)

explain

A deep copy is a copy of the operating object as a whole, which has no connection with the original copy object. After copying, a memory address is reallocated All operations are performed on the copied and the original objects are not affected

Let's talk about shallow copy

grammar

Shallow copy has a lot of syntax New object = copied object New object = copied object. copy() New object = lis copied object t0 [:] import copy New object = copy.copy (copied object)

explain

In the shallow copy, although the address of the object itself has changed, the data contained in the object is still the original address, only a reference is passed Points to the data in the original object. If you change the content of one object, the other will change accordingly (note here that if the changed data type is an unchangeable object, its reference is changed, but the data in this location is replaced by the data in another address.)

Let's take a look at the code test (the test environment of Xiaobian is Python version 3.8)


import copy

list0 = [['abc'], 'Autumn leaves and summer winds']
list1 = list0
list2 = list0.copy()
list3 = list0[:]
list4 = copy.copy(list0)
list5 = copy.deepcopy(list0)
list6 = copy.deepcopy(list0)

print('0', list0, 'Address of the copied object:', id(list0), 'Address of the first element:', id(list0[0]))
print('1', list1, 'Address of the copied object:', id(list1), 'Address of the first element:', id(list1[0]))
print('2', list2, 'Address of the copied object:', id(list2), 'Address of the first element:', id(list2[0]))
print('3', list3, 'Address of the copied object:', id(list3), 'Address of the first element:', id(list3[0]))
print('4', list4, 'Address of the copied object:', id(list4), 'Address of the first element:', id(list4[0]))
print('5', list5, 'Address of the copied object:', id(list5), 'Address of the first element:', id(list5[0]))
print('6', list6, 'Address of the copied object:', id(list6), 'Address of the first element:', id(list6[0]))

print('---------------------------------Change the contents of the copied object--------------------------------')

list1[0] = 1
list2[0] = 2
list3[0] = 3
list4[0][0] = 4
list5[0][0] = 5
list6[0] = 6

print('0', list0, 'Address of the copied object:', id(list0), 'Address of the first element:', id(list0[0]))
print('1', list1, 'Address of the copied object:', id(list1), 'Address of the first element:', id(list1[0]))
print('2', list2, 'Address of the copied object:', id(list2), 'Address of the first element:', id(list2[0]))
print('3', list3, 'Address of the copied object:', id(list3), 'Address of the first element:', id(list3[0]))
print('4', list4, 'Address of the copied object:', id(list4), 'Address of the first element:', id(list4[0]))
print('5', list5, 'Address of the copied object:', id(list5), 'Address of the first element:', id(list5[0]))
print('6', list6, 'Address of the copied object:', id(list6), 'Address of the first element:', id(list6[0]))

The results are as follows:

D:\Python38\python.exe E:/PycharmProjects/python_August/day09/On copying list.py
0 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209540405760 Address of the first element: 2209541417472
1 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209540405760 Address of the first element: 2209541417472
2 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209540405888 Address of the first element: 2209541417472
3 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209541418112 Address of the first element: 2209541417472
4 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209541417152 Address of the first element: 2209541417472
5 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209541418176 Address of the first element: 2209541417792
6 [['abc'], 'Autumn leaves and summer winds'] Address of the copied object: 2209541417856 Address of the first element: 2209541416896
---------------------------------Change the contents of the copied object--------------------------------
0 [1, 'Autumn leaves and summer winds'] Address of the copied object: 2209540405760 Address of the first element: 140708592568128
1 [1, 'Autumn leaves and summer winds'] Address of the copied object: 2209540405760 Address of the first element: 140708592568128
2 [2, 'Autumn leaves and summer winds'] Address of the copied object: 2209540405888 Address of the first element: 140708592568160
3 [3, 'Autumn leaves and summer winds'] Address of the copied object: 2209541418112 Address of the first element: 140708592568192
4 [[4], 'Autumn leaves and summer winds'] Address of the copied object: 2209541417152 Address of the first element: 2209541417472
5 [[5], 'Autumn leaves and summer winds'] Address of the copied object: 2209541418176 Address of the first element: 2209541417792
6 [6, 'Autumn leaves and summer winds'] Address of the copied object: 2209541417856 Address of the first element: 140708592568288

Process finished with exit code 0

Posted by blackwinged on Sun, 15 Mar 2020 21:08:32 -0700