473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

May 24 '07 #1
20 1647
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

May 24 '07 #2
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:

pyisinstance(False, int)
True
pyisinstance([], int)
False
pybool([])
False

James
May 24 '07 #3
>[] == 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
May 24 '07 #4
On May 23, 11:53 pm, Rajarshi <rajarshi.g...@gmail.comwrote:
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

May 24 '07 #5
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 && ma*@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
May 24 '07 #6
Rajarshi <ra***********@gmail.comwrote:
>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, ti**@probo.com
Providenza & Boekelheide, Inc.
May 24 '07 #7
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.

May 24 '07 #8
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
>>>
May 24 '07 #9
On 2007-05-24, Rex Turnbull <rex@no_spam.dicad.dewrote:
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 ...
May 24 '07 #10
In article <11**********************@q66g2000hsg.googlegroups .com>,
Paul McGuire <pt***@austin.rr.comwrote:
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....e5e1c8384c0360

Donn Cave, do**@u.washington.edu
May 24 '07 #11
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:

http://groups.google.com/group/comp....e5e1c8384c0360
If so, be sure to click "More options," then "View thread," and then
read the responses. There were many reasonable objections to her points.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Human salvation lies in the hands of the creatively maladjusted.
-- Dr. Martin Luther King, Jr.
May 24 '07 #12
On May 24, 1:59 am, Tim Roberts <t...@probo.comwrote:
....
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.
I have a job as a C++ programmer, and they make us write it like that,
apparently because the ! operator is hard to see. But "if (x ==
TRUE)" is discouraged.

May 24 '07 #13
Dan Bishop wrote:
On May 24, 1:59 am, Tim Roberts <t...@probo.comwrote:
...
>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.

I have a job as a C++ programmer, and they make us write it like that,
apparently because the ! operator is hard to see. But "if (x ==
TRUE)" is discouraged.
Find a new employer. I'm not joking.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 24 '07 #14
Steve Holden wrote:
Dan Bishop wrote:
>I have a job as a C++ programmer, and they make us write it like that,
apparently because the ! operator is hard to see. But "if (x ==
TRUE)" is discouraged.
Find a new employer. I'm not joking.
Really. He's not. That's a perfect example of a style guideline that
not only wastes energy, misses the point, but is totally wrong.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
There is another world, which is not of men.
-- Li Bai
May 24 '07 #15
Thanks a lot for all the responses

May 26 '07 #16
In article <WP******************************@speakeasy.net> ,
Erik Max Francis <ma*@alcyone.comwrote:
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:

http://groups.google.com/group/comp....e5e1c8384c0360

If so, be sure to click "More options," then "View thread," and then
read the responses. There were many reasonable objections to her points.
Not that it is of no historical interest to review all these
reasonable arguments, but allow me to restore the context quote
from my follow-up:

In article <11**********************@q66g2000hsg.googlegroups .com>,
Paul McGuire <pt***@austin.rr.comwrote:
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!
Donn Cave, do**@u.washington.edu
May 29 '07 #17
Donn Cave wrote:
Not that it is of no historical interest to review all these
reasonable arguments, but allow me to restore the context quote
from my follow-up:
If the counterpoints are of no historical interest, then the original
point must be of no historical interest either, since it was not widely
granted as true.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Fear is an emotion indispensible for survival.
-- Hannah Arendt
May 29 '07 #18
In article <cd******************************@speakeasy.net> ,
Erik Max Francis <ma*@alcyone.comwrote:
Donn Cave wrote:
Not that it is of no historical interest to review all these
reasonable arguments, but allow me to restore the context quote
from my follow-up:

If the counterpoints are of no historical interest, then the original
point must be of no historical interest either, since it was not widely
granted as true.
"Not that it is of no historical interest" may have been too
hard to follow, my apologies. I should have said "It may be of
historical interest ...". After that, you lost me, but I guess
I'm not going to worry about it.

Donn Cave, do**@u.washington.edu
May 29 '07 #19
On Tue, 29 May 2007 11:36:07 -0700, Erik Max Francis wrote:
Donn Cave wrote:
>Not that it is of no historical interest to review all these
reasonable arguments, but allow me to restore the context quote
from my follow-up:

If the counterpoints are of no historical interest, then the original
point must be of no historical interest either, since it was not widely
granted as true.
I hope you don't get just as confused by expressions like:

if not x != 5

*wink*

In English, a double negative is usually a positive. So "Not that it is of
no historical interest" means "It is of historical interest".

I wonder, if somebody with more time on their hands than me were to go
through the threads on comp.lang.python before and after the introduction
of bools, could we determine whether there were more problems caused by
the introduction of True and False than by the lack of them? Although I
like using bools, in my heart of hearts I suspect that Laura was right.
--
Steven.

May 29 '07 #20
Donn Cave wrote:
"Not that it is of no historical interest" may have been too
hard to follow, my apologies.
Yeah, my reading comprehension wasn't up to snuff that night.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
A man that studieth revenge keeps his own wounds green.
-- Francis Bacon
May 29 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sylvain Thenault | last post by:
Hi there ! Can someone explain me the following behaviour ? >>> l = >>> 0 in (l is False) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iterable argument...
3
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
15
by: F. Da Costa | last post by:
Hi all, Following two sniperts of code I'm using and getting very interesting results from. ..html <tr id="1" class="segment" open="false"> This is the segment under 'investigation' ..js
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
17
by: Tatu Portin | last post by:
Is FALSE == 0? In other words, does 'function ()' execute on all platforms: if ( (1 == 2) == 0 ) function (); if ( (7!) == 0 ) function (); I would like if you could point to some...
10
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
30
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
6
by: Java script Dude | last post by:
Small glitch (IMHO) in JavaScript is related to isNaN() boolean function. If passed a null, it returns true which is incorrect. This one cause me quite a bit of grief until I detected it and was...
14
by: Prateek | last post by:
I've been using Python for a while (4 years) so I feel like a moron writing this post because I think I should know the answer to this question: How do I make a dictionary which has distinct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.