FAQ: Stacks: Python - Stacks Python Push and Pop

This community-built FAQ covers the “Stacks Python Push and Pop” exercise from the lesson “Stacks: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Stacks Python Push and Pop

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi, I need some help to understand why:
self.top_item = item
is not equal to:
item = self.top_item

Thank you for your help!

item is a parameter, whereas self.top_item is an attribute. Attributes depend upon an owner context. Parameters are values with no context other than their position in an argument list.

3 Likes

= is not an “equals sign”. It is an assignment operator.
When it is encountered, Python

  1. Obtains a value by evaluating the expression on the right side of =
  2. Assigns that value to the variable on the left side of =

So, do you want to assign the value of item to the variable self.top_item,
… or do you want to assign the value of self.top_item to the variable item?

3 Likes

Thank you all (@mtf, @patrickd314) for your answers. Think I got it now.

1 Like

Why is it necessary to have a set_next_node() function when I can simply use

item.next_node = node

to change the value of the next connecting node?

why should i use “item = Node(value)” instead of “item = Node(self.value)” ?
what is the difference ?

It is no help to us if there is no context. You leave us guessing. Please post the URL of this exercise page.

Node is suggestive of a class object. Any instance of that would require a self in references to its variables. self.value seems more apropos.

Sorry for that . The URL :https://www.codecademy.com/paths/computer-science/tracks/linear-data-structures/modules/cspath-stacks/lessons/learn-stacks-python/exercises/stacks-python-push-and-pop
In the Push method definition
‘’’
def push(self,value):
item = Node(value)
item.set_next_node(self.top_item)
self.top_item = item
‘’’
why should i use here a “item = Node(value)” instead of “item = Node(self.value)”
thank you

1 Like

Let me look at this tomorrow. I’ve not taken this course and will need to get up to speed to be able to help. With any luck, someone might pick up the slack in the meantime.

Let me try to explain what my intuition is:

As you are method .push() takes an input of “value”, you are not using an original self.value from the class Stack, but need to input a new value to actually be able to call this method.

If you try to call the method .push() without giving it any input, it won’t work. The “value” input you give to the .push() method is what you want to put on top of your stack. This is thus not linked to your self.value.

1 Like

I was just trying to figure out is the head of the linked list removed in the pop function or it stays there as we never really set it to none in the function defined , we only set the head_node to the next_node, while the first node is still pointing to the next node, its really confusing for me trying to figure out whether the value has been removed or not

https://www.codecademy.com/paths/computer-science/tracks/linear-data-structures/modules/cspath-stacks/lessons/learn-stacks-python/exercises/stacks-python-push-and-pop

What is the difference between methods set_next_node and get_next_node in node.py? When should I use one or the other?
Why can I not simply call next_node?

Thanks

how come i write the assignment correctly, but i get an error because the compiler won’t read the function i wrote? this code returns the error " Did you define a method push() for Stack ?"

YOU CAN SEE THE METHOD IN THE CODE!

from node import Node

class Stack:

def init(self):

self.top_item = None

Define your push() and pop() methods below:

def push(self, value):

self.item = value

self.top_item = self.item

def peek(self):

return self.top_item.get_value()

EDIT: i finally gave up after 20 minutes of fighting with the compiler to find out i was missing the assignment for the node class??? HOW IS THAT THE SAME AS “have you defined the method yet?”

from node import Node as nd

class Stack:
  def __init__(self):
    self.top_item = None
  
  # Define your push() and pop() methods below:
  def push(self, value):
    item = nd(value)
  
  
  def peek(self):
    return self.top_item.get_value()

i don’t understand that why we didn’t have to insantiate Node class to use its methods in the last lesson where we just used return self.top_item.get_value() instead of return Node(self.top_item).get_value()

This is regarding the exercise Learn Stacks : Stacks Python Push and Pop, where this thread is linked.

Why do we use getters and setters to access node values instead of just accessing node_a.value directly?

Hello, @bhuveh, and welcome to the Codecademy Forums!

Using push and pop methods to perform operations on the Stack ensures that the state of the Stack is adjusted properly whenever a change is made to its contents. Since those two operations involve changing pointers to nodes, as well as managing the values at those nodes, we need to make sure that the entire operation is properly coordinated. This makes the Stack easier to use, since the user can call a single method to perform an operation.

Edited on August 17, 2020 to add the following:

Concerning the use of the get_next_node and set_next_node methods of Node, that allows for the possibility of adding to the functionality of the operations. For instance, we could decide to provide a time stamp or operate a counter when either of those operations is performed.

If programmers are using methods to perform operations, instead of using direct access, it would be simple to add to the functionality of those methods without requiring the programmers to revise all of their existing code.

1 Like

Thank you for your answer, but I apologize for not being clear.

My question is that why do we use
value1 = example_node.get_value()
instead of
value1 = example_node.value

and
example_node.set_value(value2)
instead of
example_node.value = value2

for all exercises involving nodes (not just stacks or any structure in particular).

If programmers are using methods to perform operations, instead of using direct access, it would be simple to add to the functionality of those methods without requiring the programmers to revise all of their existing code.

Is it just for adding future functionality, like you explained?

1 Like

More generally, it is to provide a standardized stable means for the user to interact with the object. In a production environment, classes and modules would be documented. Once released, programmers would create lots of code, using the standardized documented modes of access. With time, any changes in these standardized modes of access would require increasing numbers of programmers to revise their code that utilizes the objects. So, with forethought, we could provide means of access that will not need to be revised later on. As the internal details of classes and modules are upgraded over time, we would like to shelter users from those details, so that their previously written code does not also need revision. We can do this by providing access via methods, rather than having the programmer access properties directly. With methods, the public interface can be stabilized while the internal workings of the objects are refined. If instead, we had standardized access via internal details, then changing those details would create more difficulty for programmers who use the objects.

While a Node is a simple class that might not need future upgrading, a more complex class could very well be upgraded after an initial release. Accordingly, the Node class was designed as a model for more complex classes. This involves using getters and setters, or more generally, it involves the use of methods to access or alter internal information, which hides the internal details.

Edited on August 18, 2020 to add the following:

Another issue is that some interactions involve multiple steps. For example, adding a value to a data structure might include validity checks and other operations. Access via methods can guarantee that all tasks related to a transaction are coordinated correctly.

1 Like

Awesome! Thank you so much!

1 Like