FAQ: Stacks: Python - Stacks Python Size I

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

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!

Hello again!

I am working on step one of this exercise and can’t seem to get it right but I don’t want to look at the solution because the last time I did that on another exercise it wouldn’t let me try the exercise again and I learned nothing.

class Stack:
def init(self, size, limit=1000):
self.top_item = None
self.size = 0
self.limit = limit

This is my dunder init, but it keeps asking if I put “limit” as a parameter. If I take out the “=1000” it recognizes the parameter, but then asks if I set the default to 1000. I tried changing it in properties to 1000 but it says I need to set the default in the () of init. I have tried every variation I can think of and just can’t get it to work. Thanks for any help!

Hello,

You probably already got it, but for anyone looking at this in the future, here’s the code:

class Stack:
def init(self, limit=1000):
self.top_item = None
self.limit = limit
self.size = 0

Not sure why we don’t put ‘size’ in the brackets, but it worked this way.

Cheers!

I actually had not gotten it yet, so thank you so much! Yeah, it doesn’t seem to make much sense not putting size in brackets, but such is life. Thanks again!

I also have this question of why we don’t put ‘size’ as an argument.

Any moderator who can help us with the question at hand? It would be really useful for us. Thank you

1 Like

Hi everyone,

The instructions are asking us to do this for the Stack class:

  def __init__(self, limit=1000):
    self.top_item = None
    self.limit = limit
    self.size = 0

We set the instance variable size to 0 when we initialize the Stack because it starts out with no items in it. Since size always starts out at 0, we do not represent it as a parameter, as it does not need to be set via an argument when the Stack is instantiated.

We should keep in mind that the __init__ method is free to create instance variables in addition to ones that are represented by its parameters. It can also perform other tasks related to setting up an instance of the class to which it belongs.

3 Likes

Can somebody explain to me what this phrase actually means:

“Inside the method, set the instance limit property to the passed in value of limit .”

I can’t make sense of this.
Also just to say that I made the same mistake of putting size in the brackets. I understand the explanation, but it is hard to get my head around it. I suppose understanding oftern comes with practice.

That instruction is asking us to assign the value of the limit parameter to an instance variable of the same name within the __init__ method of Stack with this statement:

    self.limit = limit

In a later exercise, we will use that instance variable to constrain the size of the Stack, as follows:

  def has_space(self):
    return self.limit > self.size

The above has_space method will use the limit instance variable to inform us as to whether there is room in the Stack for us to add another item.

Thanks for that. Should it not say “Inside the method, set the instance limit property to that passed in the value of limit .” That would make more sense to me. Or is it just me?

1 Like

Your rephrasing of the instruction is accurate, and seems pretty good. Codecademy staff do read the discussion forums, and one of them might consider whether to revise that instruction for clarity.