Message from discussion
0 == False but [] != False?
Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 24 May 2007 00:14:13 -0500
Date: Wed, 23 May 2007 22:14:13 -0700
From: Erik Max Francis <m...@alcyone.com>
Organization: Alcyone Systems
User-Agent: Thunderbird 1.5.0.5 (X11/20060719)
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: Re: 0 == False but [] != False?
References: <1179982400.412340.178710@g4g2000hsf.googlegroups.com>
In-Reply-To: <1179982400.412340.178710@g4g2000hsf.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <vbKdnagrtsy6gsjbnZ2dnUVZ_vDinZ2d@speakeasy.net>
Lines: 49
NNTP-Posting-Host: 69.17.54.62
X-Trace: sv3-mqV6IqPZq2xuI1oIGVPcr5a/bYL0Ib52RByajaCCS2VZiRAxZlwoCYPcIf9LVlCGhCj491Sz8cmcVWK!ubp61hg+rqbRsbjRcE9OHOg4r4d2+6ln/CzEXJnlMQyi4UsGBzlIn4OkOTAeINMOUrzqivb58wm3!4SGor4s8mfmewUJSow1eysAn
X-Complaints-To: abuse@speakeasy.net
X-DMCA-Complaints-To: ab...@speakeasy.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.34
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