Booleans (was: Conditional operator in Python?)

Fredrik Lundh fredrik at pythonware.com
Fri Apr 6 13:04:15 EDT 2001


Erik Max Francis wrote
> 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

>>> from operator import truth
>>> truth(0)
0
>>> truth(3)
1
>>> truth('hello')
1
>>> truth('')
0
>>> truth([])
0
>>> truth([1])
1
>>> truth(None)
0
>>> print truth.__doc__
truth(a) -- Return 1 if a is true, and 0 otherwise.

Cheers /F





More information about the Python-list mailing list