[Python] Detailed explanation of python deep copy and shallow copy (must master)


Deep copy and shallow copy are the content that python must master. Whether you are interviewing for development, testing, operation and maintenance positions, as long as it is python, deep copy and shallow copy are an important knowledge point often asked by interviewers.

(Follow "Test Development Automation" Gong Zhonghao for more learning content)



1. Similarities and differences between deep and shallow copies

Same point:

Both deep copy and shallow copy will create a new object. That is: the id of the copied object is different.

difference:

Shallow copy: Only the object is copied, and the elements in the object are not copied.
Deep copy: Not only the object is copied, but the elements within the object are also copied.


Two, shallow copy

2.1 Shallow copy example

Let's look at an example of a shallow copy:


import copy
a = [1, 2, [3, 4]]       
b = copy.copy(a)    # 浅拷贝
print(id(a))        # 输出结果:2401212115720
print(id(b))        # 输出结果:2401212116104

print(id(a[2]))     # 输出结果:1508928560
print(id(b[2]))     # 输出结果:1508928560

Code explanation:
1) After b shallowly copies a, the ids of a and b objects are different (consistent with the same point)
2) Take the id of the third element of a and b respectively (the third element is a variable object), It is found that the id values ​​of the third elements of a and b are the same. It means that the id of the element is not copied, that is to say, the ids of the elements inside a and b are the same. (coincides with the difference of shallow copy)

2.2 Shallow copy modify element value

As mentioned above, the element ids inside objects a and b are the same, which means that the elements inside objects a and b are the same thing.
★Important point★: However, it does not mean that the value of the element in a is modified under shallow copy, and the value of the element in b will definitely change, depending on whether the modified value is a mutable object:

[Case 1]: If the modified element is a variable object, then the element in a is modified, the element in b will change, and the id of the element in a and b will not change; [Case 2]:
If The modified element is an immutable object, so if the element in a is modified, the element in b will not change, but the id of the element in a and b will change


# 情况1举例:修改的元素是可变对象
import copy
a = [1, 2, [3, 4]] 
b = copy.copy(a)     # 浅拷贝

a[2][1] = 4          # 修改a中[3, 4]元素,将3修改为4
print(a)             # 打印结果:[1, 2, [3, 4]]
print(b)             # 打印结果:[1, 2, [3, 4]]
print(id(a[2]))      # 打印结果:2561215900424
print(id(b[2]))      # 打印结果:2561215900424

Code explanation:
The element [3, 4] of a is a mutable object, so if the element a is modified, the element b will also be modified synchronously, but because it is a shallow copy, the ids of the elements in a and b will not change.


# 情况2举例:修改的元素是不可变对象
import copy
a = [1, 2, [3, 4]]   
b = copy.copy(a)      # 浅拷贝

a[1] = 4              # 修改a中第1个位置上的元素,即将2修改为4
print(a)              # 打印结果:[1, 4, [3, 4]]
print(b)              # 打印结果:[1, 2, [3, 4]]
print(id(a[1]))       # 打印结果:1508928624
print(id(b[1]))       # 打印结果:1508928560

Code explanation:
The first element of the a element is a numeric type (int type), which is an immutable object, so if the a element is modified, b will not change, and the elements at the first position of a and b belong to two If there are two different things, the ids of a and b are naturally not equal.


3. Deep copy

3.1 Example of deep copy


import copy

a = [1, 2, [3, 4]]
b = copy.deepcopy(a)    # 深拷贝

print(id(a))            # 输出结果:2034433010824
print(id(b))            # 输出结果:2034433010248

print(id(a[2]))         # 输出结果:2463430159112
print(id(b[2]))         # 输出结果:2463430154184

Code explanation:
1) After b shallowly copies a, the ids of a and b objects are different (consistent with the same point in 2.1)
2) Take the id of the third element of a and b respectively (the third element is a variable object ), it is found that the id values ​​of the third elements of a and b are different. It shows that a copies element b, that is to say, the element ids inside a and b are different individuals. (coincides with the difference of deep copy)

3.2 Deep copy modification value

Contrary to shallow copy, deep copy can also be divided into two cases:

[Case 1]: If the modified element is a variable object, then the element in a is modified, the element in b will not change, and the id of the element in a and b will change; [Case 2]:
If The modified element is an immutable object, so if the element in a is modified, the element in b will not change, but the id of the element in a and b will change (same as shallow copy)


# 情况1举例:修改的元素是可变对象
import copy
a = [1, 2, [3, 4]] 
b = copy.deepcopy(a)     # 浅拷贝

a[2][1] = 4          # 修改a中[3, 4]元素,将3修改为4
print(a)             # 打印结果:[1, 2, [3, 4]]
print(b)             # 打印结果:[1, 2, [3, 4]]
print(id(a[2]))      # 打印结果:1975929744136
print(id(b[2]))      # 打印结果:1975929739208

Code explanation:
The element [3, 4] of a is a mutable object, which is a deep copy, so if the a element is modified, the b element will not be modified synchronously. Because it is a deep copy, modifying the variable object element in a will not change the id of the element in b.


Welcome to like + follow + collect

(Follow "Test Development Automation" Gong Zhonghao for more learning content)

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_44244190/article/details/129249875