Python深浅拷贝

导入模块import copy

names = ['lilei','hanmei',['lll','rrrr']]

深拷贝

names2 = copy.deepcopy(names)

浅拷贝2中方式 只拷贝一层

1

names2 = copy.copy(names)

2

names2 = names[:]

names[1] = 'HANMEI'
names[2][0] = 'KKKKK'
print(names)
print(names2)

浅拷贝打印:

['lilei', 'HANMEI', ['KKKKK', 'rrrr']]
['lilei', 'hanmei', ['KKKKK', 'rrrr']]

深拷贝打印:

['lilei', 'HANMEI', ['KKKKK', 'rrrr']]
['lilei', 'hanmei', ['lll', 'rrrr']]

你可能感兴趣的:(Python深浅拷贝)