标签:
http://www.jb51.net/article/15714.htm
1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。
2. copy.deepcopy 深拷贝 拷贝对象及其子对象
一个很好的例子:
1 import copy 2 a = [1, 2, 3, 4, [‘a‘, ‘b‘]] #原始对象 3 4 b = a #赋值,传对象的引用 5 c = copy.copy(a) #对象拷贝,浅拷贝 6 d = copy.deepcopy(a) #对象拷贝,深拷贝 7 8 a.append(5) #修改对象a 9 a[4].append(‘c‘) #修改对象a中的[‘a‘, ‘b‘]数组对象 10 11 print (‘a = ‘, a) 12 print (‘b = ‘, b) 13 print (‘c = ‘, c) 14 print (‘d = ‘, d) 15 16 #运行结果 17 ‘‘‘ 18 a = [1, 2, 3, 4, [‘a‘, ‘b‘, ‘c‘], 5] 19 b = [1, 2, 3, 4, [‘a‘, ‘b‘, ‘c‘], 5] 20 c = [1, 2, 3, 4, [‘a‘, ‘b‘, ‘c‘]] 21 d = [1, 2, 3, 4, [‘a‘, ‘b‘]] 22 ‘‘‘
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
标签:
原文地址:http://www.cnblogs.com/linkenpark/p/5167840.html