你能在 Python 中澄清这个引用问题吗?

Can you clarify this referencing issue in Python?

我习惯了指针和引用,但我需要对这一点进行一些说明:

我的节点中有这个方法class:

def createNewNode(graph):
    #Create New Node Dictionary
    newNode = {}
    # Fill in Details for that Node
    newNode['NAME'] = 'Max'
    newNode['AGE'] = 22
    newNode['ID'] = 'DC1234SH987'
    #Add the Node to the Graph
    graph.addNode(newNode)

创建的newNode范围在函数createNewNode()内。现在此节点已添加到图中的节点列表 class。

图表 class 具有此功能:

def addNode(self, node):
    self.nodeList.append(node)

现在图 class 函数 addNode() 只是将节点附加到图 class 中的节点列表中。在节点 class 中调用 graph.addNode() 后,newNode 变量的范围何时停止存在?

Graph class 中附加到列表中的数据现在不会无效吗? append() 是否为传递的对象制作新副本?你能给我解释一下吗?

(在这个例子中,图表只包含一个节点列表,节点实际上是一个包含详细信息的字典。)

名称newNode超出范围,但它引用的对象没有被销毁。您通过将其附加到 nodeList 来添加对该对象的另一个引用。 append 不会创建副本,但会创建对同一对象的单独引用。 name 在函数结束时超出范围,但 object 仅在 all[= 时才被垃圾收集18=] 对它的引用消失了。