Sorry if this is a stupid question, but I think I'm missing some crucial knowledge on how classes work in Python.
Say, for example, that I'm re-inventing the wheel by implementing a list data structure. Then I would have created a Node-class that contains data and pointer to the next node. I would also have List class that contains some methods for the list, like Append, Remove etc.
Now, what I'm struggling with is how can I manipulate instances of these list Nodes.
My node class is as
class Node:
def __init__(self,next,data)
self.next = next
self.data = data
If I want to find the last node of the list, I would look something like this
current_node = list.header
while current_node.next != None:
last_node = current_node
Now, I want wanted to do something with the selected last_node, I would do something like
last_node.data = "some data"
#or
del(last_node)
Now, does doing anything with the "last_node" variable actually modify the original instance of the class, or did I create a new node?
If I want to do something like
that_node = this_node.next
del(that_node)
Does it delete the node after "this_node", or is this code pointless? There are some situations where I feel assigning a new variable is necessary, for example, if I'm iterating over several nodes and "this_node" might be changing.
I hope this makes any sense!
del
variables explicitly; rather you write functions which do your tasks, and variables declared in the function disappear by themselves due to limited scoping.