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

2 Answers

Hi Yu-che,

As far as I understand the question, it only evaluates if the line

Orange().squeeze()

will return True.

That means, if it will return True, we choose to answer "True" from the quiz options.

And if the line does not return True, "False" being the correct quiz answer, that does not necessarily mean, that the line already returns False, opposing your main question "Why this will return false"

In fact, it does not return False. It returns a NameError (which is a valid condition to select "False" as the answer):

>>> from fruits import Orange
>>> Orange().squeeze()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\fruits.py", line 8, in squeeze
    return has_pulp
NameError: name 'has_pulp' is not defined
# fruits.py
class Fruit:
    has_pulp = False

class Orange(Fruit):
    has_pulp = True

    def squeeze(self):
        return has_pulp

This is because the the squeeze() method which is called by the instance does not know the class variable has_pulp. It only knows the attribute of the instance that called it which is why self.pulp is valid.

Hope this helps. :blush:

Hi,

From the question:

class Orange(Fruit):
   has_pulp = True

    def squeeze(self):
        return has_pulp

Orange().squeeze() will return True.

The mistake is that in function,

    def squeeze(self):

the value returned was

        return has_pulp

instead of

        return self.has_pulp
Yu-Che Hung
Yu-Che Hung
Courses Plus Student 10,607 Points

Anne thank you But why? why I have to add "return self.has_pulp" so that it will return True?