Welcome to the Treehouse Community
Looking to learn something new?
light_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.
Dave StSomeWhere19,822 Points
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
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