[Python-Dev] For review: PEP 285: Adding a bool type

Tim Peters tim.one@comcast.net
Sat, 09 Mar 2002 15:27:10 -0500


[David Abrahams]
> ...I guess I can see why you're really intending that
>
>     >>> 0 or False
>     False
>
> but
>
>     >>> False or 0
>     0
>
> This sure rubs all my expectations for a bool the wrong way, though.

It has much more to do with expectations for what "and" and "or" do.  Note
that they're not "operators" in Python, they're control structures, and
cannot be overridden.  Don't mix bools with ints with control structures,
and you won't get surprised; "False or 0" returning 0 in Python is no more
surprising than that

if False:
    x = False
else:
    x = 0

sets x to 0, and *oodles* of code relies on that equivalence.

> I think I'd better just write my own assertion routine, as Guido
> suggested. I don't like non-obvious language constructs for something so
> simple (I'd mention ?: here but I don't need the bruises).

Na, do this:

def bool(e):
    return e and 'True' or 'False'

Then wrap your true/false expressions in bool() calls.  All assuming
x-version invariance is important to you.  When versions of Python before
2.3 become uninteresting, get rid of the bool() function (and so unmask the
builtin of the same name).