[Python-ideas] Programming recommendations (PEP 8) and boolean values
Stephen J. Turnbull
stephen at xemacs.org
Thu Aug 9 09:33:36 CEST 2012
Ben Finney writes:
> Oleg Broytman <phd at phdru.name> writes:
> > On Thu, Aug 09, 2012 at 02:18:53AM +1000, Ben Finney <ben+python at benfinney.id.au> wrote:
> > > What is a compelling use case for checking precisely for True or False?
> > To distinguish False and None for a tri-state variable that can
> > have 3 values - "yes", "no" and "unspecified" - True, False and None
> > in Python-speak.
> Since True and False are strongly coupled with a *two*-state type, that
> just seems needlessly confusing to the reader of the code. Better to use
> a non-bool type which makes it explicit that there are three valid
I occasionally use an abstract base class that initializes a Boolean
member to None, and assert "is not None" in testing or validation
code. I wouldn't call it "compelling," but I find this convention
very useful in validating my own code, as if such variables weren't
properly initialized in a derived class, I probably screwed up
something else, too.
More information about the Python-ideas