Python:深拷贝,浅拷贝import copy


import copy as cp
a = ['hedeniu','xiaoniuniu','xiaoxiaoniu',4,['a','b'],5,6]

b = a #赋值,传对象的引用
c = cp.copy(a)  #对象拷贝,浅拷贝
d = cp.deepcopy(a) #对象拷贝,深拷贝

a.append(10) #修改对象a
a[4].append('c') #修改对象a中的['a','b']数组对象

print("a=:",a)
print("b=:",b)
print("c=:",c)
print("d=:",d)

运行结果:

a=: ['hedeniu', 'xiaoniuniu', 'xiaoxiaoniu', 4, ['a', 'b', 'c'], 5, 6, 10]
b=: ['hedeniu', 'xiaoniuniu', 'xiaoxiaoniu', 4, ['a', 'b', 'c'], 5, 6, 10]
c=: ['hedeniu', 'xiaoniuniu', 'xiaoxiaoniu', 4, ['a', 'b', 'c'], 5, 6]
d=: ['hedeniu', 'xiaoniuniu', 'xiaoxiaoniu', 4, ['a', 'b'], 5, 6]



你可能感兴趣的:(Python学习)