Hi everyone, my question is regarding the garbage collection mechanism in python. What do I mean?
My understanding is that an object will be “garbage collected” if it is in isolation, that is, if it points to nothing AND nothing points to it.
Now, if you think about the head node of the list, it is an object that points to the next node (assume for a moment there is one) but is not pointed by any other node, right? We don’t expect the head node to be “garbage collected”.
Now, when we have the list a -> b -> c and we decide we want to move b, for example, we have the following states:
current_node.set_next_node(next_node.get_next_node()) - this will give us a -> c, and b -> c, and currNode -> a
current_node = None - this will give us a -> c, b -> c and currNode being null and not pointing to anything.
According with this memory picture, node b is not going to be “garbage collected” since it still points to node c. That is why, when I wrote my solution I had:
current_node.set_next_node(next_node.get_next_node()) next_node.set_next_node(None) #this line kills the b -> c, and now it is really "alone" - not pointing to anything, nor being pointed by anything current_node = None
Is there something I don’t get about the garbage collection mechanism?