首页 > 其他 > 详细

浅拷贝和深拷贝

时间:2018-04-27 14:35:33      阅读:211      评论:0      收藏:0      [点我收藏+]
浅拷贝和深拷贝的区别?

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

例如:

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


浅拷贝和深拷贝

原文:http://blog.51cto.com/13043937/2108510

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!