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

Python Inheritance Question

I ran into this question in the Python OOP course:

class Orange(Fruit):

has_pulp = True

def squeeze(self):
    return has_pulp

Orange().squeeze() will return True.

A. True

B. False

the answer is False. I tested the code in PyCharm and it returned NameError: name 'Fruit' is not defined.

I wonder if this is the reason why this is False, because we didn't define the parent class?

Also, I am not sure if it is correct to write "Orange().squeeze()" instead of "Orange.squeeze()"

Thanks for your time!

1 Answer

Steven Parker
Steven Parker
243,253 Points

The issue here is that since the "squeeze" method returns "has_pulp" and not "self.has_pulp" we don't have any information to really know what it will return. It might return True if there's a global variable (not shown here) that was also set to True. It could just as easily have been set to False. Or perhaps it is not defined and the function will cause a NameError exception like in your version.

So since we can't say for sure that it will return True, the statement itself is then false.