Message from discussion
0 == False but [] != False?
Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!spamkiller2.gnilink.net!gnilink.net!trndny09.POSTED!2bd68fb6!not-for-mail
From: Tim Roberts <t...@probo.com>
Newsgroups: comp.lang.python
Subject: Re: 0 == False but [] != False?
Organization: Providenza & Boekelheide, Inc.
Message-ID: <1rda53hphn117k5scvcqrc7c2e37utfhgu@4ax.com>
References: <1179982400.412340.178710@g4g2000hsf.googlegroups.com>
X-Newsreader: Forte Agent 4.2/32.1117
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 30
Date: Thu, 24 May 2007 06:59:32 GMT
NNTP-Posting-Host: 71.117.238.113
X-Complaints-To: abuse@verizon.net
X-Trace: trndny09 1179989972 71.117.238.113 (Thu, 24 May 2007 02:59:32 EDT)
NNTP-Posting-Date: Thu, 24 May 2007 02:59:32 EDT
Rajarshi <rajarshi.g...@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, t...@probo.com
Providenza & Boekelheide, Inc.