At the end of python PEP8 I'm reading:
Don't compare boolean values to True or False using
==
Yes: if greeting: No: if greeting == True: Worse: if greeting is True:
I have no problem with that recommandation when the boolean is True
, but it sounds strange when checking for False
If I want to know if variable greeting is False why shouldn't I write:
if greeting == False:
If I write if not greeting:
it will have a very different meaning that the above statement. What if greeting is None ? What if it is empty string ? Does this PEP8 recommandation means that variables storing boolean values should only contains True or False and that None should be avoided for these variables ?
To my eyes it looks like a recommandation coming from other languages with static typing and that does not fit well with python, at least for comparing to False.
And by the way, does anyone know why if greeting is True:
is described as worse that if greeting == True:
? Should we also understand that if greeting is False:
is also worse that if greeting == False:
?