0

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!

4
  • 2
    Mandatory link to Ned Batchelder
    – quamrana
    Commented Jun 26 at 9:34
  • Run the code at pythontutor.com and see for yourself.
    – deceze
    Commented Jun 26 at 10:08
  • Note that you usually don't del variables explicitly; rather you write functions which do your tasks, and variables declared in the function disappear by themselves due to limited scoping.
    – deceze
    Commented Jun 26 at 10:09
  • Thanks quamrana for that document, was a good read. I understand that assign with = does not actually copy anything, it's just an address that points to the same place in memory.
    – Adalwolf
    Commented Jun 26 at 10:13

1 Answer 1

0

First, your loop is missing a crucial part:

current_node = list.header
while current_node.next != None:
  last_node = current_node

You don't use .next field properly. Try this:

current_node = list.header
while current_node != None:
  last_node = current_node
  current_node = current_node.next

Now to your question.
You can check the identity of an instance with id():

n1 = Node(None, 1)
n2 = Node(None, 1)
print(id(n1)) # prints some number, like 4344546448
print(id(n2)) # prints some number, like 4344549280

The two nodes have different IDs, so they are not the same instance.

We can compare IDs with is:

n3 = n1
print(n1 is n2) # False
print(n1 is n3) # True

With this knowledge you can answer your question.

The = doesn’t create a new instance of Node. It points the variable to the existing instance.

2
  • 1
    The while loop should be last = list.header; while last.next is not None: last = last.next.
    – deceze
    Commented Jun 26 at 10:14
  • @deceze you are right but OP wanted a current_node variable for an unknown reason. I only wanted to fix the mistakes, not to refactor it
    – Noam-N
    Commented Jun 26 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.