Home > Article > Backend Development > Python copy objects (deep copy deepcopy and shallow copy copy)
1. copy.copy shallow copy only copies the parent object, and does not copy the internal child objects of the object.
2. copy.deepcopy deep copy object and its sub-objects
A good example:
import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy.copy(a) #对象拷贝,浅拷贝 d = copy.deepcopy(a) #对象拷贝,深拷贝 a.append(5) #修改对象a a[4].append('c') #修改对象a中的['a', 'b']数组对象 print 'a = ', a print 'b = ', b print 'c = ', c print 'd = ', dOutput result:
d = [1, 2, 3, 4, ['a', 'b ']]
For more Python copy objects (deep copy deepcopy and shallow copy copy) related articles, please pay attention to the PHP Chinese website!