True/false integer values

Erik Max Francis max at alcyone.com
Fri May 2 18:04:29 EDT 2003


Carsten Gehling wrote:

> Does that mean, that the boolean
> "true" value in Python is 1 and not -1?

Yes.  In pre-2.3 versions, the Python's "true" value is a numerical 1. 
(From 2.3 on, Python actually has a Boolean type, which if coerced into
an integer will equal 1.)

The theory behind making the "true" value in a language -1 is usually
because that's the integer all bits on (in 2's complement, anyway), and
if your logical operators are really bitwise operators in disguise, then
this makes sense (for instance, in some versions of BASIC).  If,
however, you have separate logical operators that do short-circuiting,
like Python and many other modern languages do, there's no need for the
distinction.

You could just check yourself, after all:

[pre-2.3]
>>> 1 == 1
1
>>> 1 == 2
0

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The basis of optimism is sheer terror.
\__/ Oscar Wilde
    HardScience.info / http://www.hardscience.info/
 The best hard science Web sites that the Web has to offer.




More information about the Python-list mailing list