Python拷贝分为深拷贝和浅拷贝
- 浅拷贝对子对象不拷贝,深拷贝全部拷贝
l1 = [1, 2, [3, 4]]
l2 = copy.copy(l1)
l1.append(5) l1[2].append(5) # 子对象 改变 print(l1) print(l2) -------------- [1, 2, [3, 4, 5], 5] [1, 2, [3, 4, 5]]
- 深拷贝完是两个完全不相干的对象
l1 = [1, 2, [3, 4]]
l2 = copy.deepcopy(l1)
l1.append(5) l1[2].append(5) print(l1) print(l2) -------------- [1, 2, [3, 4, 5], 5] [1, 2, [3, 4]]
本文首发于python黑洞网,博客园同步更新