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 Basic Object-Oriented Python Welcome to OOP Methods

need help with 'if self.is_moving' and 'if not self.is_moving'

at 5:40 in this video the instructor runs through the code with an explanation but i can't get to grips with the two if statements in the different methods.

i don't understand what the if statements are checking for? how does if self.is_moving and if not self.is_moving both run when self.is_moving = False, are they not checking for a different boolean value in each if statement?

3 Answers

Hi aaron,

You are right in that self.is_moving and not self.is_moving will return two different Boolean values and as such will trigger at different times.

But I’m thinking that maybe you believe that self.is_moving is always and will always be False. But self.is_moving is changed by the stop and go methods, so sometimes it will be False and other times it will be True.

Does that clear up your confusion, or have I misinterpreted your question?

thanks! so the if statement itself changes the boolean value? could you explain what the if statements are doing and how they are interpreting self.is_moving, i don't think I've come across this before?

Hi Aaron,

I’ll do my best.

So self is the instance of the class.

So if you write something like this:

my_car = Car() # I can’t remember what the parameters are so I’m not passing in any args here
my_car.go(fast)

self is my_car (or more specifically it’s the value stored into the my_car variable.

So in the go method, where the if statement tests the condition if not self.is_moving, what’s happening is the python interpreter is testing where my_car.is_moving is True or False. If the value for is_moving is False, the not operator makes the test evaluate to True, and then the if statement’s code block is executed where self.is_moving (my_car.is_moving) has the Boolean stored to it changed from False to True. If the value for is_moving is/was True, the not operator would make the test evaluate to False, and the code block would NOT be executed.

To be clear, the not operator is not what changes the value of self.is_moving, it simply reverses the Boolean that is the result of evaluation of the expression/condition.


And self refers to the instance of the class. So the prototype for the go function is def go(self, speed):.

Think of my_car.go(‘fast’) as if it were my_car.go(my_car, ‘fast’), because that’s essentially what happening.

The first positional argument of every method defined within a class must be self, because the instance of that class is automatically passed as an argument into that method.

So the self.is_moving that is declared and initialized in the init method in creation of the Car object, is the same one that’s being referenced and changed within the stop and go methods.

I tried to cover it as in depth as I can think to. Does that help you in your understanding?

Thanks again, but referring to this line: "If the value for is_moving is False, the not operator makes the test evaluate to True".

The way i am interpreting 'if not self.is_moving' is 'if not false' meaning the code should only execute if 'self.is_moving = True' but i must be wrong as is does execute? can you see where i am looking at it wrong?

Thanks

OK. Let’s try breaking down the way the if condition works.

The code block in an if condition only ever runs if the condition evaluated to true or truthy. Truthy would be like if you had an if statement that went:

if 5: # do something

5 is not a Boolean so it doesn’t really evaluate to True or False, but it’s a number other than 0, which makes it truthy in that scenario.

So back to the if condition. In order for the code block of an if statement to execute, the condition must evaluate to True/truthy. That’s simply the design of the programming language/programming in general.

So this raises the question, “What if I only want the code block to run in a case where the expression would evaluate to False?” You could make an if...else statement with two code blocks, and for the if block you could write pass while writing the code you actually want to run in the else block. But that would be messy.

The way around it is to change a False value to True after the evaluation. That’s what not does. So in the example you only want a specific piece of code to run if is_moving is False. But if you just write:

if is_moving:
    # do something when False

Then your code block is never executed when the car isn’t moving (which is the opposite of what you want). Adding the not operator prior to the expression says no matter what, if is_moving is False, make the condition True so the code block runs. And if is_moving is True, make the condition False so the code block doesn’t run.

The other way you could do it would be to write: if is_moving is False: as that expression would evaluate to True when the value of is_moving is False.

Does that clear it up for you?

your a legend mate, thanks!