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黑洞網,博客園同步更新
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/164055.html
標籤:Python
上一篇:Python- 裝飾器
