[Python-ideas] Break the dominance of boolean values in boolean context

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Sep 14 02:46:41 CEST 2011

Guido van Rossum wrote:
> Control flow just *is* special. FWIW It is not
> bool values that are being treated special; it is certain bool
> operators (not, and, or). I see nothing wrong with that; you can think
> of them as an extension of the repertoire of 'if', 'while' etc.

Seems to me they're something of a mixture of computation
and control flow. The computation consists of calling the
__nonzero__ methods of the operands to find out whether
they should be considered true or false.

PEP 335 doesn't change that, it just shifts the emphasis
slightly. It provides hooks allowing you to do more
computation before deciding on the control flow.

> De Morgan's law (and similar transformation) seem to me out of scope.
> The would violate gut feelings about the "natural" execution order of
> Python expressions.

I don't see how. If e.g. you transform

   if not (a and b):


   if not a or not b:

then the evaluation order doesn't change -- you still
evaluate a first, and then evaluate b if needed. It's
just an extension of the existing rewriting of 'not'
deeper into the expression.


More information about the Python-ideas mailing list