Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Inheritance Inheritance Quiz

Joshua Hovden
Joshua Hovden
2,979 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
8,568 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.