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 trialSebastiaan Stoffels
3,075 PointsObject Oriented Python - Inheritance Quiz
Hi there, Just making my way through the new Object Oriented Python course, and wanted to check my understanding on something. I'm on the section covering Inheritance, and on Quiz question 4 of 5 the following question is posed:
The following code:
class Orange(Fruit):
has_pulp = True
def squeeze(self):
return has_pulp
Orange().squeeze() will return True
The answer is false, but I want to know why. The only thing I could find is that the function definition for squeeze() is missing the 'self' part, i.e. it should be:
def squeeze(self):
return self.has_pulp
In which case, it would return True
Am I right that is the reason? Or is there another 'learning' from this particular question.
3 Answers
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsSelf refers to the current instance of the class. In your first code, the has_pulp without self is not the same vaiable as the class variable definded earlier. With self.has_pulp now refers to the class variable.
hamdi ismail
Courses Plus Student 8,927 Pointsno you are right if it was self.has_pulp then it will be true
Ikuyasu Usui
36 PointsIt gives error right? Orange() is not a class, but an instance acutally: Orange is a class while Orange() is an instance. But the Orange class doesn't have self.has_pulp, so asking for it produces an error. I think Orange.squeeze() doesn't work either because you need to pass an instance of Orange. So Orange.squeeze(Orange()) works. Am I correct on all these?
Ikuyasu Usui
36 PointsOrange.squeeze(Orange()) doesn't work. I guess making attribute like self.attribute is important.