FAQ: Stacks: Python - Stacks Python Introduction

This community-built FAQ covers the “Stacks Python Introduction” 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 Introduction

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’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.

Can anyone explain?

class Stack:
  def __init__(self):
    self.top_item = None
    
  def peek(self):
    return self.top_item.get_value()
3 Likes

Since the node course, they are doing quite weird things…
It looks like they try to make simple things more complicated.

i also got confused about that…

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

1 Like

@mtf Can you please explain this?

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:

from time import ctime
print(ctime())

from node import Node
class Stack:
  def __init__(self, top_item= None):
    self.top_item = top_item

  def peek(self):
    return self.top_item.get_value()


my_stack = Stack(Node(10))
print(my_stack.peek())

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.