0 == False but [] != False?
Raymond Hettinger
python at rcn.com
Thu May 24 01:09:48 EDT 2007
> >>> [] == False
> False
>
> Could anybody point out why this is the case?
Writing, "if x" is short for writing "if bool(x)".
Evaluating bool(x) checks for a x.__nonzero__()
and if that method isn't defined, it checks for
x.__len__() to see if x is a non-empty container.
In your case, writing "if []" translates to
"if len([]) != 0", which evaluates to False.
True and False are of type bool which is a subclass
of int. So, False really is equal to zero and
True really is equal to one.
In contrast, the empty list is not of type int.
So [] != False eventhough bool([]) == False.
Raymond
More information about the Python-list
mailing list