Booleans (was: Conditional operator in Python?)

Erik Max Francis max at alcyone.com
Fri Apr 6 12:36:24 EDT 2001


"Russell E. Owen" wrote:

> However, the broader issue is of a separate boolean type used for all
> logical operations. I personally think it's quite messy and confusing
> that any value has an associated logical value (e.g. 0 is false, any
> other integer is true, "" is false, any other string is true, etc.).
> But
> as I said in my previous posting, it's a widely used construct that
> doesn't seem to screw up languages too badly. And there's no way to
> change this in Python without breaking tons of code.

This is true.  I would be mollified if there were a builtin operator
that tests truth in the same way that if ...: does.  It would be named
something like bool or boolean and would return a 0 or 1 and nothing
else:

>>> bool(0)
0
>>> bool(3)
1
>>> bool('hello')
1
>>> bool('')
0
>>> bool([])
0
>>> bool([1])
1
>>> bool(None)
0

I'm actually surprised nothing like that already exists in the language
(maybe it does and I'm just missing it), since being able to test for
truth in the same way that if ...: would seem a common application.

That at least makes the conditional operator workaround somewhat less
ugly:

    [a, b][bool(x)]

> Regarding your boolean class, could you explain the value to adding
> "don't know" to it? I can imagine it has its uses, but it sounds like
> a
> different problem domain than standard conditional logic such as
> if/then/else. If you're aiming for fuzz logic then presumably you'd
> want
> more choices than simply true, false and don't know.

Agreed, it has limited application, so limited in fact that someone
needing such behavior (true, false, don't know) would be better off
writing their own Boolean type that behaved the way they wanted.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ All the people in her neighborhood turn around and get mad and sing
\__/ Public Enemy
    Alcyone Systems' CatCam / http://www.catcam.com/
 What do your pets do all day while you're at work?  Find out.



More information about the Python-list mailing list