Web Images Videos Maps News Groups Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion 0 == False but [] != False?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rex Turnbull  
View profile  
 More options May 24 2007, 9:21 pm
Newsgroups: comp.lang.python
From: Rex Turnbull <rex@no_spam.dicad.de>
Date: Thu, 24 May 2007 13:21:50 +0200
Local: Thurs, May 24 2007 9:21 pm
Subject: Re: 0 == False but [] != False?
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
 >>>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google