I know that this question has been asked a bunch of times (for this I apologize), and PEP8 "covers" it by saying that if x is True
<< x == True
<< if x
, but I still don't understand why. It is my understanding that both None
and True
are singletons in Python, so I don't quite see the nuance. This especially confuses me because PEP8 says:
Comparisons to singletons like None should always be done with is or is not, never the equality operators.
I also understand that the if x
syntax covers a few more cases than if x is True
in that if x
can do things like check for a non-empty string/list/etc or a non-zero number, but in the case where you know x
is a boolean value, why is it so incorrect to compare with is
?
Is the reason that PEP8 assumes that if x
is safer because it covers the case that if x is True
covers and those other cases I referenced above?