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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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.
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.
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
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?
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()
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.
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?
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.