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 trialHanna Han
4,105 Pointslight_level < 10
Hi, I don't get this following part in the def hide method because the teacher doesn't explain how it works and why we use for what.
def hide(self, light_level): return self.sneaky and light_level < 10
Is it the way we run the class/method by using hide method like hide(X) without writing self together in the parentheses like hide(self, X) and if so, why?
How does "and light_level < 10" work in the code?
I think the video should be more explanations for beginners in python.
1 Answer
Dave StSomeWhere
19,870 PointsHello Hanna,
If you check the teachers notes on that video you see it starts with:
"All of the same rules apply to method arguments as they do to function arguments, with the exception that the first argument has to represent the instance. You can use default values, *args, and **kwargs all you want!"
To answer your first question - no, we don't specify self
when running/calling the class method. I think of it as a built in feature of classes - so that the instance details are automatically passed as the first argument (kind of behind the scenes stuff).
Now, for your second question - how does "and light_level < 10" work in the code. The return
statement is evaluating an expression and returning either True
or False
. Remember the sneaky property is a Boolean and is set to True in the first statement. You pass a light_level argument to the method and the return statement will evaluate the expression and return either True or False. When light level is passed as 4 - (sneaky = True and (4 is < 10 = True)) - return True
When light level is passed as 12 - (sneaky = True and (12 is < 10 = False)) - return False
Hope that helps
Hanna Han
4,105 PointsHanna Han
4,105 PointsThank you, Dave. Your explanation helped a lot!