I'm used to pointers and references, but I need some clarification on this point:
I have this method in my Node 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)
The scope of the newNode
created is within the function createNewNode()
. Now this Node is added to a list of nodes in the Graph class.
The Graph class has this function:
def addNode(self, node):
self.nodeList.append(node)
Now the Graph class function addNode()
just appends the node to the list of nodes in the graph class. After the call to graph.addNode()
in the node class, when does the scope of the newNode
variable stop existing?
Won’t the data that was appended to the list in the Graph class be invalid now?
Does append()
makes a new copy of the object passed? Can you explain this to me?
(In this example, the graph just contains a list of nodes and the node is actually a dictionary with details.)