[Tutor] comparison bug in python (or do I not get it?)

bob gailer bgailer at alum.rpi.edu
Sat Mar 1 19:27:00 CET 2008

Ricardo Aráoz wrote:
> Just one further question :
>  >>> 1 == True
> True
>  >>> 5 == True
> False
> and yet
>  >>> if 5 : print 'True'
> True
> I thought a non-zero or non-empty was evaluated as True. Now in the 5 == 
> True line I'm not saying "5 is True", shouldn't it evaluate just like 
> the "if" statement?
Python Library Reference: 3.1 Truth Value Testing:

Any object can be tested for truth value, for use in an if or while 
condition or as operand of the Boolean operations below. The following 
values are considered false:

zero of any numeric type, for example, 0, 0L, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a __nonzero__() 
or __len__() method, when that method returns the integer zero or bool 
value False.3.1

All other values are considered true -- so objects of many types are 
always true.

Bob Gailer
919-636-4239 Chapel Hill, NC

More information about the Tutor mailing list