Go to Google Groups Home    comp.lang.python
Re: 0 == False but [] != False?

Raymond Hettinger <pyt...@rcn.com>

> >>> [] == 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