At the end of Python PEP 8 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 recommendation when the Boolean is True
, but it sounds strange when checking for False
.
If I want to know if a variable greeting is False, why shouldn't I write the following?
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 an empty string? Does this PEP 8 recommendation 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 recommendation 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, why is if greeting is True:
described as worse than if greeting == True:
? Should we also understand that if greeting is False:
is also worse that if greeting == False:
?