Booleans (was: Conditional operator in Python?)

Bruce Sass bsass at freenet.edmonton.ab.ca
Mon Apr 2 15:31:34 EDT 2001


On Mon, 2 Apr 2001, Russell E. Owen wrote:
>  Erik Max Francis <max at alcyone.com> wrote:
> >...
> >On a vaguely related subject, why is there no Boolean type in Python?
> >Seems like it would clean things up a great deal -- my understanding is
> >that there is even an internal Python Boolean type, but it is hidden in
> >the interpreter.  How come?
>
> I suspect the basic reason is because C doesn't have one. But that's
> just a guess.

I figured it was because a "boolean" is more theoretical than anything
else, or at least never the whole story.  Tri-state would be much more
useful -- true, false, don't know -- but what do you do in the case of
"don't know"... use a default truth value, the tv of another object
(determined at the time the boolean is created, or when the tv is
being looked at?), or provide a hook and let the coder handle it
(ending up with different classes of booleans)?

> I personally dislike the concept that "every variable has a boolean
> value", and the associated fact that utterly different values have the
> same logical value. But hey, a large numer of languages work this way
> and it doesn't seem to cause too much trouble. (Though I think Python
> would benefit from a built in function that tests for logical equality
> -- if I'm missing one, please enlighten me; the best I've come up with
> so far is: not a == not b, which is not exactly obvious.)

If you use 0 == false and 1 == true, the bit-wise exclusive-or is a
negative logic version of what you want:

A B  xor
0 0   0
0 1   1
1 0   1
1 1   0

I coded up a tri-state "Boolean" class awhile back that supports
all the logical operations, truth-value testing, and "mixed mode"
boolean arithmetic (* == and, + == or)... I could clean it up and make
it public if anyone is interested (although a consensus on the
questions I posed above would be helpful, since that is where I got
wishy-washy before I moved onto something else).

> But even if the Python development team suddenly became convinced that a
> separate boolean type was the way to go, it'd be quite a challenge to
> implement such a thing without breaking a vast amount of existing code.

Why would adding something to the language break old code...
just don't change the old behaviour.


- Bruce





More information about the Python-list mailing list