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!

3 Likes

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!

3 Likes

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.

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

Thank you, this is a really helpful explanation for these concepts in general.

1 Like

I don’t understand how we can calculate the size of the stack when there is no code was written to count number of nodes. we just set in the beginning the self.size = 0. How the code is supposed to count the number of nodes. And then we subtract -1 from the base which equals 0. Don’t get it?

def pop(self):
if self.size > 0:
item_to_remove = self.top_item
self.top_item = item_to_remove.get_next_node()
self.size -=1
return item_to_remove.get_value()
else:
print(‘The stack is empty’)

1 Like

Hi @andriymalenkov499496,

By the end of 4. Stacks Python Size II, you will have this in the push method …

      self.size += 1

… and this in the pop method …

      self.size -= 1

On that basis, you will be able to use the size instance variable to get the number of nodes that contain data in any instance of Stack.

1 Like

Thanks, I’ve figured that out, once I jumped to the next episode. I would suggest you, guys, to make it clear at the beginning

3 Likes

Hi! I understand about size it starts at 0 we don’t represent it as a parameter, but why the exercise is asking to do it?

size should be set to 0 in __init__() .

The size variable is created for keeping track of how many items are in the Stack instance. It always should start at 0 at the time that we create the instance, so the __init__ method is the appropriate place for initializing it.

In the __init__ method, we can perform whatever tasks are necessary for properly setting up an instance of a class.