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!
Hi, I’m confused about how my Stack class can use methods from the node class, since I never defined a stack as an instance of a node? In the solution code, we call get_value() from the node class, despite being in the body of a Stack class.
from node import Node
# Add your Stack class below:
class Stack:
def __init__(self):
self.top_item = Node(None)
def peek(self):
return self.top_item.get_value()
sta = Stack()
print(sta.peek())
I think were forgetting to wrap the value in a Node class
I really didn’t understand this one. We call the get_value method on self.top_item but we haven’t defined self.top_item as anything? Surely if we’re going to use a Node method on it it has to be a Node class to start out with? I don’t understand why the peek method isn’t just Node(self.top_item).get_value()
At the top of your script, you imported the Node class and its methods.
Since you did that, you can now use a Node class method as you want in your stack Class.
Like you always did with differents modules importations:
My best guess is:
At the stage of definition of Class, invoking a method from another class is acceptable. As long as an instance of Node is created, triggering the get_value method works fine. But triggering it before creating instance of Node throws an error.
By the way, importing Node at the top, doesn’t mean that we can use any mothod of it. Access mothed should in form of Node.method.