Home > Blockchain >  Simplifying an 'if' statement with bool()?
Simplifying an 'if' statement with bool()?

Time:05-07

I am a beginner python user. how would I shorten this? my pylint was complaining.

def eat_ghost(power_pellet_active, touching_ghost):
    if power_pellet_active is True and touching_ghost is True:
        return True
    return False

CodePudding user response:

you don't need to write is True because what this does is check if True is True. You can also return the result of the x and y directly, it is again a boolean value.

def eat_ghost(power_pellet_active, touching_ghost):
    return power_pellet_active and touching_ghost

CodePudding user response:

if expression evaluates to if bool(expression).

but you should be aware that bool(expression) is not equal to expression is True. bool("any non empty string") is True but "any non empty string" is True is False

  • Related