Message from discussion
0 == False but [] != False?
Path: g2news1.google.com!news1.google.com!news.glorb.com!newsfeed0.kamp.net!newsfeed.kamp.net!newsfeed.freenet.de!news.tiscali.de!newsfeed.hanau.net!news-fra1.dfn.de!feeder.pironet-ndh.com!news.pironet-ndh.com!not-for-mail
From: Rex Turnbull <rex@no_spam.dicad.de>
Newsgroups: comp.lang.python
Subject: Re: 0 == False but [] != False?
Date: Thu, 24 May 2007 13:21:50 +0200
Organization: PiroNet NDH AG
Lines: 42
Message-ID: <f33sgd$31t$1@news.pironet-ndh.com>
References: <1179982400.412340.178710@g4g2000hsf.googlegroups.com> <1rda53hphn117k5scvcqrc7c2e37utfhgu@4ax.com> <pan.2007.05.24.10.50.56.763316@REMOVE.THIS.cybersource.com.au>
NNTP-Posting-Host: ws184.dicad.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.pironet-ndh.com 1180005709 3133 195.94.70.184 (24 May 2007 11:21:49 GMT)
X-Complaints-To: support@pironet-ndh.com
NNTP-Posting-Date: Thu, 24 May 2007 11:21:49 +0000 (UTC)
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
In-Reply-To: <pan.2007.05.24.10.50.56.763316@REMOVE.THIS.cybersource.com.au>
Steven D'Aprano :
> On Thu, 24 May 2007 06:59:32 +0000, Tim Roberts wrote:
>
>> As a general rule, I've found code like "if x == False" to be a bad idea in
>> ANY language.
>
>
> Surely that should be written as "if (x == False) == True"?
>
>
Why compare to False?
" if not x : ... "
It really doesn't matter if x is False or if it evaluates to False. Many
things evaluate to False like [], (), 0, "", None and a few other things.
>>> def tf(thing):
... if thing : print "True thing", thing
... elif not thing : print "False thing",thing
... else : print "No thing"
...
>>> tf([])
False thing []
>>> tf([1])
True thing [1]
>>> a = ()
>>> tf(a)
False thing ()
>>> a=(0)
>>> tf(a)
False thing 0
>>> a= (1,2,3)
>>> tf(a)
True thing (1, 2, 3)
>>> tf("abc")
True thing abc
>>> tf("")
False thing
>>>