question of style

Piet van Oostrum piet at
Fri Jul 17 10:12:52 CEST 2009

>>>>> koranthala <koranthala at> (k) wrote:

>>> That test was designed to treat None as a boolean False, without
>>> noticing that numeric 0 is also treated as False and could make the
>>> test do the wrong thing.  This is an extremely common type of error.

>k> Actually, I felt that 0 not being considered False would be a better
>k> option.
>k> I had lot of instances where 0 is a valid value and always I had to
>k> put checks specifically for that.
>k> For me, None and False should be equal to False in if checks, every
>k> thing else being considered True.

>k> Can someone let me know why 0 is considered equal to False?
>k> There should be real good reasons for it, only that I cannot think of
>k> one.

Originally Python didn't have booleans. Integers were used instead (but
not exclusively). 0 was False, everything else True (with a slight
preference for 1 as True). Same as in C. Nowadays this is reflected in
bool being a subtype of int with False == 0 and True == 1. Actually it
is even closer: False and True are just 0 and 1 cast to bool, so to say.
Piet van Oostrum <piet at>
URL: [PGP 8DAE142BE17999C4]
Private email: piet at

More information about the Python-list mailing list