Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects Method Arguments

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

  1. 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?

  2. 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
Dave StSomeWhere
19,822 Points

Hello 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

Thank you, Dave. Your explanation helped a lot!