Google Groups Home
Help | Sign in
0 == False but [] != False?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  21 messages - Collapse all
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
Rajarshi  
View profile
 More options May 24 2007, 2:53 pm
Newsgroups: comp.lang.python
From: Rajarshi <rajarshi.g...@gmail.com>
Date: 23 May 2007 21:53:20 -0700
Local: Thurs, May 24 2007 2:53 pm
Subject: 0 == False but [] != False?
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?

Thanks,
Rajarshi


    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.
Robert Kern  
View profile
 More options May 24 2007, 3:05 pm
Newsgroups: comp.lang.python
From: Robert Kern <robert.k...@gmail.com>
Date: Thu, 24 May 2007 00:05:02 -0500
Local: Thurs, May 24 2007 3:05 pm
Subject: Re: 0 == False but [] != False?

"if foo:" does not check if "foo == True" or "foo == False" but rather
"bool(foo)". For empty lists, strings, tuples, dicts and some other things,
"bool(foo) == False", while for lists, etc., with at least one element,
"bool(foo) == True".

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


    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.
James Stroud  
View profile
(1 user)  More options May 24 2007, 3:06 pm
Newsgroups: comp.lang.python
From: James Stroud <jstr...@mbi.ucla.edu>
Date: Wed, 23 May 2007 22:06:31 -0700
Local: Thurs, May 24 2007 3:06 pm
Subject: Re: 0 == False but [] != False?

Meditate on:

py> isinstance(False, int)
True
py> isinstance([], int)
False
py> bool([])
False

James


    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.
Raymond Hettinger  
View profile
(1 user)  More options May 24 2007, 3:09 pm
Newsgroups: comp.lang.python
From: Raymond Hettinger <pyt...@rcn.com>
Date: 23 May 2007 22:09:48 -0700
Local: Thurs, May 24 2007 3:09 pm
Subject: Re: 0 == False but [] != False?

> >>> [] == False
> False

> Could anybody point out why this is the case?

Writing, "if x" is short for writing "if bool(x)".
Evaluating bool(x) checks for a x.__nonzero__()
and if that method isn't defined, it checks for
x.__len__() to see if x is a non-empty container.

In your case, writing "if []" translates to
"if len([]) != 0", which evaluates to False.

True and False are of type bool which is a subclass
of int.  So, False really is equal to zero and
True really is equal to one.

In contrast, the empty list is not of type int.
So [] != False eventhough bool([]) == False.

Raymond


    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.
Paul McGuire  
View profile
 More options May 24 2007, 3:11 pm
Newsgroups: comp.lang.python
From: Paul McGuire <pt...@austin.rr.com>
Date: 23 May 2007 22:11:22 -0700
Local: Thurs, May 24 2007 3:11 pm
Subject: Re: 0 == False but [] != False?
On May 23, 11:53 pm, Rajarshi <rajarshi.g...@gmail.com> wrote:

This has *got* to rank up there among the VFAQ's of them all, along
with the mysterious shared default empty list argument.  I think this
particular question has been asked in one form or another at least
twice a week for the past month!

-- Paul


    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.
Erik Max Francis  
View profile
 More options May 24 2007, 3:14 pm
Newsgroups: comp.lang.python
From: Erik Max Francis <m...@alcyone.com>
Date: Wed, 23 May 2007 22:14:13 -0700
Local: Thurs, May 24 2007 3:14 pm
Subject: Re: 0 == False but [] != False?

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


    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.
Tim Roberts  
View profile
 More options May 24 2007, 4:59 pm
Newsgroups: comp.lang.python
From: Tim Roberts <t...@probo.com>
Date: Thu, 24 May 2007 06:59:32 GMT
Local: Thurs, May 24 2007 4:59 pm
Subject: Re: 0 == False but [] != False?

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.


    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.
Steven D'Aprano  
View profile
 More options May 24 2007, 8:51 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE.THIS.cybersource.com.au>
Date: Thu, 24 May 2007 20:51:02 +1000
Local: Thurs, May 24 2007 8:51 pm
Subject: Re: 0 == False but [] != False?

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"?

--
Steven.


    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.
Rex Turnbull  
View profile
(1 user)  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.
Grant Edwards  
View profile
 More options May 25 2007, 12:14 am
Newsgroups: comp.lang.python
From: Grant Edwards <gra...@visi.com>
Date: Thu, 24 May 2007 14:14:37 -0000
Local: Fri, May 25 2007 12:14 am
Subject: Re: 0 == False but [] != False?
On 2007-05-24, Rex Turnbull <rex@no_spam.dicad.de> wrote:

> 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?

That's a joke... I say, that's a joke, son!

He was being sarcastic.  

--
Grant Edwards                   grante             Yow! The FALAFEL SANDWICH
                                  at               lands on my HEAD and I
                               visi.com            become a VEGETARIAN ...


    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.
Donn Cave  
View profile
 More options May 25 2007, 3:10 am
Newsgroups: comp.lang.python
From: Donn Cave <d...@u.washington.edu>
Date: Thu, 24 May 2007 10:10:15 -0700
Local: Fri, May 25 2007 3:10 am
Subject: Re: 0 == False but [] != False?
In article <1179983482.452458.188...@q66g2000hsg.googlegroups.com>,
 Paul McGuire <pt...@austin.rr.com> wrote:

> This has *got* to rank up there among the VFAQ's of them all, along
> with the mysterious shared default empty list argument.  I think this
> particular question has been asked in one form or another at least
> twice a week for the past month!

Anyone who finds this surprising, might enjoy reading this
article from the time several years ago when the feature
was being considered.  When you have some time - it's long,
but interesting.  The present confusion is more directly
addressed towards the end.  Yes, it's the Laura Creighton
article again:

http://groups.google.com/group/comp.lang.python/msg/2de5e1c8384c0360

   Donn Cave, d...@u.washington.edu


    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.
Erik Max Francis  
View profile
 More options May 25 2007, 5:54 am
Newsgroups: comp.lang.python
From: Erik Max Francis <m...@alcyone.com>
Date: Thu, 24 May 2007 12:54:01 -0700
Local: Fri, May 25 2007 5:54 am
Subject: Re: 0 == False but [] != False?

Donn Cave wrote:
> Anyone who finds this surprising, might enjoy reading this
> article from the time several years ago when the feature
> was being considered.  When you have some time - it's long,
> but interesting.  The present confusion is more directly
> addressed towards the end.  Yes, it's the Laura Creighton
> article again:

>