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

Erik Max Francis <m...@alcyone.com>

Rajarshi wrote:
> This is a slightly naive question, but I know that 0 can be used to
> represent False. So

>>>> 0 == False
> True

> But, I know I can use [] to represent False as in

>>>> if not []: print 'empty'
> ...
> empty

> But then doing the following gives a surprising (to me!) result

>>>> [] == False
> False

> Could anybody point out why this is the case?

Because "representing False" (i.e., being false) and "being the same as
False" are not the same thing.

        if x:
           ...

is not the same thing as

        if x == True:
            ...

it's the same as

        if bool(x):
            ...

So a more meaningful comparison of your two tests are:

 >>> bool(0) == bool(False)
True
 >>> bool([]) == bool(False)
True

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
   Woman was God's _second_ mistake.
    -- Friedrich Nietzsche