0 == False but [] != False?

Tim Roberts timr at probo.com
Thu May 24 02:59:32 EDT 2007


Rajarshi <rajarshi.guha at gmail.com> 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?

False is just a constant.  0, (), '', [], and False are all constants that
happen to evaluate to a false value in a Boolean context, but they are not
all the same.

As a general rule, I've found code like "if x == False" to be a bad idea in
ANY language.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list