Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。2. copy.deepcopy 深拷贝 拷贝对象及其子对象
一个很好的例子:
http://www.cnblogs.com/Images/OutliningIndicators/None.gifimport copy
http://www.cnblogs.com/Images/OutliningIndicators/None.gifa = [1, 2, 3, 4, ['a', 'b']]#原始对象
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gifb = a#赋值,传对象的引用
http://www.cnblogs.com/Images/OutliningIndicators/None.gifc = copy.copy(a)#对象拷贝,浅拷贝
http://www.cnblogs.com/Images/OutliningIndicators/None.gifd = copy.deepcopy(a)#对象拷贝,深拷贝
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gifa.append(5)#修改对象a
http://www.cnblogs.com/Images/OutliningIndicators/None.gifa[4].append('c')#修改对象a中的['a', 'b']数组对象
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gifprint 'a = ', a
http://www.cnblogs.com/Images/OutliningIndicators/None.gifprint 'b = ', b
http://www.cnblogs.com/Images/OutliningIndicators/None.gifprint 'c = ', c
http://www.cnblogs.com/Images/OutliningIndicators/None.gifprint 'd = ', d
输出结果:
a =, 5]
b =, 5]
c =]
d =]
页:
[1]