[Python-ideas] Programming recommendations (PEP 8) and boolean values

Ben Finney ben+python at benfinney.id.au
Wed Aug 8 18:18:53 CEST 2012

Michael Foord <fuzzyman at gmail.com> writes:

> True and False are singletons, so if you want to check precisely for
> True and False then an identity check seems appropriate.

What is a compelling use case for checking precisely for True or False?
Why is a simple boolean expression (‘if foo’) not good enough?

I can't think of any code checking precisely for True or False that
wouldn't be much improved by using different values altogether. But
maybe you have code examples that would be convincing.

> 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.

The distinction is real and important, but that doesn't make both sides
useful or that one shouldn't be deprecated.

> 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.

What bugs do you have in mind?

 \          “I see little commercial potential for the Internet for at |
  `\                               least ten years.” —Bill Gates, 1994 |
_o__)                                                                  |
Ben Finney

More information about the Python-ideas mailing list