On 08/08/2012 09:11 AM, Michael Foord wrote:
True and False are singletons, so if you want to check precisely for True and False then an identity check seems appropriate.
However the "Programming Recommendations" section of PEP 8 has this to say on the topic:
Don't compare boolean values to True or False using ==.
Yes: if greeting: No: if greeting == True: Worse: if greeting is True:
It seems to me that there is an important distinction between testing that an object is either the boolean True or False and merely checking the "truthiness" of an object. Many a bug has been caused by an empty container object (or some other falsey object) falling into an "if not value" clause that was actually meant to check for the presence of False or None.
Why does PEP 8 recommend not testing "boolean values" (which to me implies "values you expect to be a bool") using equality or identity, and more specifically why does it say that using an identity check is worse than an equality check?
Near the top of the web page you referred is ...
This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.
I think in the context of coding Python's library, the "if greeting:" case makes the function more general and easier to use in more situations.