Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Inheritance Inheritance Quiz

Joshua Hovden
Joshua Hovden
3,299 Points

I don't understand why Orange().squeeze() wouldn't return True

Don't Orange objects have the has_pulp attirbute equal to True and doesn't the squeeze method return has_pulp?

2 Answers

boi
boi
14,241 Points

The has_pulp is being returned through the squeeze function but is an attribute outside of the squeeze function.

class Orange:
    has_pulp = True

    def squeeze(self):
        return has_pulp

>>> Orange().squeeze()
#Traceback
#NameError: name 'has_pulp' is not defined 
 class Orange:
    def squeeze(self):
        has_pulp = True
        return has_pulp

>>> Orange().squeeze()
True

Ran into the same issue on this problem and took me a while to figure out

From testing it there is a missing self on the has_pulp in the squeeze method.

To get the code running correctly make the following change to the original code:

class Orange(Fruit):
    has_pulp = True

    def squeeze(self):
        return self.has_pulp

Then run the python repl and see if that works.