简单点说
1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。id会变化
2. copy.deepcopy 深拷贝 拷贝对象及其子对象。id会变化
>>> import copy >>> a=[1,2,3,[4,5,6]] >>> b=a #普通赋值 >>> c=copy.copy(a) #浅拷贝 >>> d=copy.deepcopy(a) #深拷贝 >>> print(a,b,c,d) [1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]] #------------------------------------------------------------------------------ >>> a[3].append("chen") >>> print(a) [1, 2, 3, [4, 5, 6, ‘chen‘]] >>> print(b) [1, 2, 3, [4, 5, 6, ‘chen‘]] >>> print(c) [1, 2, 3, [4, 5, 6, ‘chen‘]] >>> print(d) [1, 2, 3, [4, 5, 6]] #------------------------------------------------------------------------------ >>> a.append("wang") >>> print(a) [1, 2, 3, [4, 5, 6, ‘chen‘], ‘wang‘] >>> print(b) [1, 2, 3, [4, 5, 6, ‘chen‘], ‘wang‘] >>> print(c) [1, 2, 3, [4, 5, 6, ‘chen‘]] >>> print(d) [1, 2, 3, [4, 5, 6]]
dd
时间: 2024-10-06 23:23:21