浅拷贝和深拷贝

浅拷贝和深拷贝的区别?

深拷贝无论有多少嵌套都会复制出来

例如:

import copy

# 题目

list01 = [44, 55, 66]

list02 = [11, 22, 33, list01]

list03 = list02  # 直接赋值

list04 = list02.copy()  # 浅拷贝-copy

list05 = copy.copy(list02)  # 浅拷贝 - copy

list06 = copy.deepcopy(list02)  # 深拷贝 - deepcopy

 

# 修改List01

list01[0] = 88

list02[0] = 99

 

print(list01)

print(list02)

print(list03)

print(list04)

print(list05)

print(list06)

 

执行结果:

C:\python\python.exe C:/python/demo/file3.py

[88, 55, 66]

[99, 22, 33, [88, 55, 66]]

[99, 22, 33, [88, 55, 66]]

[11, 22, 33, [88, 55, 66]]

[11, 22, 33, [88, 55, 66]]

[11, 22, 33, [44, 55, 66]]

 

Process finished with exit code 0


猜你喜欢

转载自blog.51cto.com/13043937/2108510