FAQ: Stacks: Python - Stacks Python Push and Pop

So, when I first do these lessons I’m using VSCode to suss out the solution and this was the initial code I used.

def push(self, value):
    item = Node(value, self.top_item)
    self.top_item = item

When Codecademy generated the error I realize you wanted this:

def push(self, value):
    item = Node(value)
    item.set_next_node(self.top_item)
    self.top_item = item

And, from your explanation, I take it both work equally well at doing exactly what is being asked. However, if we wanted to include something like print(“Node added.”) later on, putting that into the .set_next_node() method is preferable to doing it in .push(), right?

Hello @coreblaster01537,

As you are developing the code for a data structure, use calls print where you need them for debugging purposes. After refinement is complete, remove the calls to print when they are no longer necessary for the proper functioning of the data structure.

For a general purpose data structure, such as a stack, you probably would not want to permanently include calls to print, because the output would likely become a liability. Therefore, after debugging is complete, and you are using the data structure to perform a task, use external calls to print to test and debug the code for performing that task.

Following is code that might be used for debugging a calculator that uses a stack:

# functions for debugging the calculator
def push_with_report(val, stack):
  stack.push(val)
  print(f"Pushed: {val} ")

def pop_with_report(stack):
  val = stack.pop()
  print(f"Popped: {val} ")
  return val

calc = Stack()
push_with_report(7, calc)
push_with_report(4, calc)
val = pop_with_report(calc)

Output:

Pushed: 7 
Pushed: 4 
Popped: 4

Edited on November 12, 2020 to add the following:

As a general principle, use print calls to first test the lower level components of a system, Then, as the lower level components are perfected, progressively move the use of print calls to higher level components that are being tested.

1 Like

Just wondering/noticing, the pop method doesn’t actually “remove” a node in this exercise, right?
It just returns data.

The code of the pop method:

  def pop(self):
    item_to_remove = self.top_item
    self.top_item = item_to_remove.get_next_node()
    return item_to_remove.get_value()

When we want to remove nodes, we don’t actually delete them in these exercises. Rather, say the node we want to remove is node_x. We assign the link of node_x's previous node to node_x's following node. Example follows.

Before Removal

nodes: node_w | node_x | node_y
links: node_x | node_y | node_z

After Removal

nodes: node_w | node_x | node_y
links: node_y | node_y | node_z

Since node_x is no longer accessible because there’s no way to navigate through it since node_w has reassigned its next_node, it is essentially “removed”.


We remove the link to item_to_remove in the pop() method, then return its value. So, in short, we do remove the node from the stack in addition to returning its value.

2 Likes

Oh yes! I forgot about how nodes link and how that effects things. Thank you for this insight!

1 Like