man*_*del 20 python dictionary deep-copy
我想知道在以下背景下,深度复制是如何工作的:
from copy import deepcopy
def copyExample:
self.myDict = {}
firstPosition = "First"
firstPositionContent = ["first", "primero"]
secondPosition = "Second"
secondPositionContent = ["second"]
self.myDict[firstPosition] = firstPositionContent
self.myDict[secondPosition] = secondPositionContent
return deepcopy(self.myDict)
def addExample(self):
copy = self.copyExample()
copy["Second"].add("segundo")
Run Code Online (Sandbox Code Playgroud)
它是否返回对字典中列表的引用?或者它是否按预期工作并复制具有不同引用的新列表中的每个列表?
我知道什么是深层复制(所以没有必要解释深层和浅层之间的区别),但我想知道它是否像我期望的那样工作,因此在我使用时不会更改实例变量addExample().