Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 0 == False but [] != False?

Reply
Thread Tools

0 == False but [] != False?

 
 
Rajarshi
Guest
Posts: n/a
 
      05-24-2007
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 With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      05-24-2007
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?


"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 With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      05-24-2007
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?
>
> Thanks,
> Rajarshi
>


Meditate on:

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

James
 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      05-24-2007
> >>> [] == 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 With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      05-24-2007
On May 23, 11:53 pm, Rajarshi <(E-Mail Removed)> 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?
>
> Thanks,
> Rajarshi


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 With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      05-24-2007
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 && http://www.velocityreviews.com/forums/(E-Mail Removed) && 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 With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      05-24-2007
Rajarshi <(E-Mail Removed)> 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, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-24-2007
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 With Quote
 
Rex Turnbull
Guest
Posts: n/a
 
      05-24-2007
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 With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      05-24-2007
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 With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CustomValidator Returns False, but False is Ignored DJ ASP .Net 3 12-27-2007 10:29 AM
debug="false" in web.config and <%@ debug="true" ...%> in aspx file => true or false? André ASP .Net 3 08-28-2006 12:02 PM
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM
false instance and false class and set_trace_func trans. (T. Onoma) Ruby 1 09-25-2004 06:01 PM
false instance and false class and set_trace_func trans. (T. Onoma) Ruby 0 09-25-2004 05:34 PM



Advertisments