How to write stack constructor

The question that I am stuck is below. I am learning a stack. The question I have is, according to what I have learned up until now, the constructor should be as this

 def __init__(self, value=None):
      self.top_item = Node(value)
 def peek(self):
      return self.top_item,get_value()

But below self.top_item is set to none, yet still uses method attribute of Node. This is what throws me off.

from node import Node
# Add your Stack class below:
    class Stack:
      def __init__(self):
        self.top_item = None
      def peek(self):
        return self.top_item.get_value()

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

A stack isn’t a singleton (contain exactly one), having a constructor accept a single value to populate the stack with does not make sense

you could accept an iterable and iterate through it and populate the stack with those values. you might have to give some thought to whether they should be inserted in reverse order as a stack is first-in-last-out

you could accept zero arguments, making it only possible to instantiate an empty stack which would then have to be populated separately

both of those might contradict codecademy, and if that’s the case then codecademy is likely suggesting something that isn’t correct

having None as a default value for a single value is also not appropriate, because putting None in the stack is completely valid

you could accept a variable number of arguments, populating the stack with each of them. this might be compatible with what codecademy suggests, while still being correct. it would however not match how other python data structures behave
in the case of missing an argument you would get zero argument, not None, so you would be able to tell the difference

no it doesn’t. you cannot peek on an empty stack

like I said in that other thread. if you have no values, you have no values. no matter how you implement it, you cannot obtain the top value of an empty stack.

your linked list is either a cell, or empty. a cell and empty have different valid operations. before you decide to do an operation like looking at the first value, you have to first determine whether your list is empty

this is true for a regular python list as well

a solution to the problem of that an operation might not have a result, is to wrap the result in a result type. this would make the operation always valid, because failure would yield a lack of result

stack = Stack.empty()
first = stack.peek()
if first:
    print(first.value)
else:
    print('there is no first value')

sometimes None is used instead, but there’s a very big problem with that, which is that you can’t tell None (missing) apart from None (found), which you can if you have a result type:

Some(None)  <-- found a None
Nothing     <-- no result

A list can also be used as such a result type - you could return an empty list for missing, or a list containing the result

[None]  <-- found a None
[]      <-- no result

python tends to deal with it using exceptions:

[None].pop()  # None
[].pop()      # IndexError - empty list (no result)