# FAQ: Stacks: Python - Stacks Python Size II

This community-built FAQ covers the “Stacks Python Size II” exercise from the lesson “Stacks: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Stacks Python Size II

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.

Agree with a comment or answer? Like () to up-vote the contribution!

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!

I got a message for the very last part that asked: Does the size return True when it is equal to 0? My answer was yes, but it didn’t think so. I got fed up and looked at the solution.

MY CODE

``````  # Define has_space() and is_empty() below:
def has_space(self):
if self.limit > self.size:
return True
else:
return False

def is_empty(self):
if self.size == 0:
return True
else:
return False
``````

THE SOLUTION

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

def is_empty(self):
return self.size == 0
``````

It’s clear to me that the solution is a better way to do it, but my code seemed to work for the has_space, but not for the is_empty. Why?

Shouldn’t these be equivalent? Am I wrong? What do you think?

Returning boolean literals is the naive approach to coding, which this course is well beyond, preferring return expressions over literals.

``````self.limit > self.size

self.size == 0
``````

are both boolean expressions.

1 Like

I understand my method isn’t the best way and the solution is definitely better.

If i understand what you are saying. Technically my code should work (that it is equivalent), but it’s so redundant that the exercise didn’t accept it?

Redundant might not be the appropriate word. Naive is the word I chose. The SCT author wants us to move away from the beginner style to the intermediate. Technically there is nothing wrong with your code as it stands, just that it uses literals instead of expressions.

2 Likes

As I understand, Stacks not always have a limit. In this case, should function has_space take this option into consideration, so it does not raise an error when limit = None?

This is a good explanation and I understand about moving us learners to the next stage. It is a bit frustrating though when the instructions actually say, “The method should return `True` if the stack’s `size` is 0.”

One thing I’ve found about coding is that every day is a school day.

3 Likes

It is frustrating because it allows the “if… return True” statement in the has_space method, but disallows it in the is_empty method, and offers no explanation as to why. With us basically left wondering why this code won’t work. I understand if the author is trying to move us past such naive statements, but this isn’t really made clear, or even explained at all in the lesson.

Just looking at dates, it’s interesting that I’m having the same issue that was discussed two years ago.

in fact even redoing the lesson copy/pasting the solution doesn’t help in my case.

Solution:

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

def is_empty(self):
return self.size == 0

My code:

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

def is_empty(self):
return self.size == 0

Both get me the same error message when I try it, but it is literally a copy paste from the solution that lets you finish the page.

I am getting strange errors on this one too

I have noticed I am having to do this more lately, that is to clear the code and start the exercise from scratch. It usually works after I do this, and I know that I change nothing because I just copy the code into notepad and paste it back in.

Here is my code(only the `has_space()` and `is_empty()` methods):

has_space = lambda self : self.limit > self.size is_empty = lambda self : self.size is 0

Can I improve it?

When declaring a parameter in a lambda, it will be local only to the function. `self` will not be `self`, the class instance.

Can you make it so that `self` refers to the class like when you use `def`? You can just type the classes name if you want to call the method. What if you can call this method like this. If you didn’t send me that post then I would use the `has_space()` method like this:
`stack = Stack(10) stack.has_space()`
How do you make these lines of code go into multiple lines? How many backticks?
Will this throw an error? I think it would because you have to pass an argument that corresponded to `self` unlike in an equivalent `def` where the method thinks it refers to the class. I now think that because you said `self` was a required argument because the `lambda` doesn’t refer the first parameter as the class.

Can you explain this more? This code is meant to be in a class.

What we call our instance within the class is arbitrary, with the convention being to give the name, `self`. We could call it `this` or `that` or anything we wish. Once we define the name with `__init__()` (give it attributes), then `self` becomes the instance object itself (by local reference).

``````def is_empty(self):
return self.size < 1
``````

Given an instance, `a`,

``````a.is_empty()
``````

Will report back on that instance. Is that what your lambdas do, as well? If that is the case, then ignore my earlier comment. Perhaps it’s just that one is not used to seeing lambdas as instance methods.

I haven’t tested my `is_empty()` method. Why can’t the `lambda` method refer the self parameter to the instance?

How commonly are `lambda`s used to make methods? Why is/isn’t it a good practice to do that?

print("1 2") # Four spaces between 1 and 2. print("1 2") # A manual tab between 1 and 2. print("1 2") # Six spaces. print("1\t2") # A coded tab.

Why does 4 spaces take the same space as a manual tab? Why does 6 spaces take the same space as a coded tab? Why are the amount of spaces different? Why does a manual tab take a different amount of space as a coded tab?