首页 > 技术文章 > copy&deepcopy

themost 2017-03-21 07:49 原文

 1 >>> import copy
 2 >>> list1 = [1,2,3,['a','b']]
 3 >>> list2 = copy.copy(list1)#浅复制,修改子对象将受影响
 4 >>> list3= copy.deepcopy(list1)#深复制,修改子对象不受影响
 5 >>> list2
 6 [1, 2, 3, ['a', 'b']]
 7 >>> list3
 8 [1, 2, 3, ['a', 'b']]
 9 >>> list1[3].append('c')
10 >>> list1
11 [1, 2, 3, ['a', 'b', 'c']]
12 >>> list2
13 [1, 2, 3, ['a', 'b', 'c']]
14 >>> list3
15 [1, 2, 3, ['a', 'b']]

 

推荐阅读