https://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html
#!/usr/bin/python
# -*-coding:utf-8 -*-
import copy
a = [1, 2, 3, 4, ['a', 'b']] #原始对象
b = a #赋值,传对象的引用
c = copy.copy(a) #对象拷贝,浅拷贝
d = copy.deepcopy(a) #对象拷贝,深拷贝
a.append(5) #修改对象a
a[4].append('c') #修改对象a中的['a', 'b']数组对象
print( 'a = ', a )
print( 'b = ', b )
print( 'c = ', c )
print( 'd = ', d )
运行结果:
('a = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5]) ('b = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5]) ('c = ', [1, 2, 3, 4, ['a', 'b', 'c']])? ?#浅copy,数组会被影响 ('d = ', [1, 2, 3, 4, ['a', 'b']])? ? ? ? #深copy,数组不会被影响
浅copy 的时候,如果有子对象不是基本类型,那个子对象被修改的话,所有引用这个子对象的都会被影响。
|