0 == False but [] != False?
Robert Kern
robert.kern at gmail.com
Thu May 24 01:05:02 EDT 2007
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?
"if foo:" does not check if "foo == True" or "foo == False" but rather
"bool(foo)". For empty lists, strings, tuples, dicts and some other things,
"bool(foo) == False", while for lists, etc., with at least one element,
"bool(foo) == True".
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list