What is truth? - part 1 - false vs zero

14 July 2007

Iverson VS Boolean?

In my recent post, Python CGI contact forms, I open sourced my little contact form processor I use on my webpage and solicited comments from you, the reader.

David Jones left some really great comments, thanks Dave! The first comment went as follows:

> This is a very minor niggle about your python code: > > Whilst Python is an Iverson's convention language, it is more modern style to use [return False] and [return True] instead of [return 0 and return 1]. Of course it's up to you what versions of Python to support, but True and False have been in since 2.3.

What is Iverson's convention? Well fortunately, David also left a post on his own blog where he explored that. To take the first paragraph:

> Iverson's Convention is, roughly speaking, the idea that a boolean result can be encoded using the first two natural numbers: 0 for false, 1 for true.

While I personally would attribute that to George Boole himself, if not to the ancient Greeks; use of the term Iverson, however, is useful as David uses the terms Iverson and boolean to distinguish the two ways to go. It is a quite good way of clarifying things tidily.

While we on the subject, George Boole is a fascinating as a man and not many people know it about him, so I think I should perhaps do my next post about that, if anyone is interested in something quite different.

Anyway, so BASIC and C use Iverson (1 and 0), while Java uses true and false instead. Python is a little more complex, but David tells us that True and False have been more fashionable since Python 2.3. I have always just used 1 and 0 since I did BASIC before and the numbers are quicker to type. However, I thought I should look into that to see whether I think truth should be 1, or truth is true!

The first step was to log into three different computers with different versions of Python, and see what the interpreter said.

Who is the coolest cow?

Python 2.2 - Solaris

> Python 2.2.3 (#1, Sep 6 2003, 09:33:14) > > [GCC 2.95.3 20010315 (release)] on sunos5 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> "cow" == "chicken" > > 0 > > >>> holstein_friesian = "cow" > > >>> "cow" == holstein_friesian > > 1

Python 2.4 - Gentoo

> Python 2.4.4 (#1, May 9 2007, 01:10:49) > > [GCC 3.4.6 (Gentoo Hardened 3.4.6-r2, ssp-3.4.6-1.0, pie-8.7.9)] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> "cow" == "chicken" > > False > > >>> aberdeen_angus = "cow" > > >>> "cow" == aberdeen_angus > > True

Python 2.5 - Ubuntu

> Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) > > [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> "cow" == "chicken" > > False > > >>> polled_hereford = "cow" > > >>> "cow" == polled_hereford > > True

So the modern interpreters certainly prefer True and False.

What would Guido do?

I wanted to look at some actual Python code to see what is cool there. There are probably a million ways to do this, but my approach was to run the following bash command in a directory of Python code:

grep -l "return True" *.py | wc -l

This gives a number for how many files have 'return True'. Do the same for 'return False', 'return 0' and 'return 1' then we have a rough guide to how popular each is. This might overestimate the 0 and 1, if in rare cases, you actually do use the number as a return value in a numerical calculation.

My sample was the Python standard library. I only bothered with the root of the lib directory, I did not bother to go recursively down through subdirectories.

To start with, the Iverson Convention, i.e. '1 and 0' is completely dominant, here are the figures for Python 2.2 on Solaris:

Search Term Number of Files return True 0 return False 1 return 1 38 return 0 40

At the time of writing, the latest version of Python is 2.5.1, and the following results are from the same place as before (root of the Lib directory. These results are from the vanilla source tarball:

Search Term Number of Files return True 39 return False 41 return 1 23 return 0 24

We can see that between versions, i.e. Python 2.2 and Python 2.5, the balance has moved from one to the other. There is now twice as many files using 'return True' rather than 'return 1'. Therefore I should take David's suggestion seriously and consider using True rather than 1.

In a future post I will look at this from a completely different direction, so stay tuned.

1 Dan says...

Pretty cool article. I only just started using Python, and the book had listed True and False, so that's what I've been using. I didn't even think to use 1 and 0.

Posted at 3:15 p.m. on July 14, 2007


What do you have to say?

Show Editing Help

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

http://kratosellas.blogspot.com

September 8, 2010
http://kratosellas.blogspot.com ?????????? ?????????
Bremen Travel Snaps

kristengreen

September 8, 2010
Hello,http://www.jumancuso.info - May i sex dating with you?
OOXML Vote Coverage

play solitaire online

September 8, 2010
Thanks for sharing this link, but unfortunately it seems to be down... Does anybody have a mirror or another source? Please answer to my post if you do! I would ...
Better Python Console 0.2.5

tixartich

September 7, 2010
http://thesecretstofarmville.com - Farmville Cheats
Burning an iso to CD on Windows

kristengreen

September 7, 2010
Hello,http://www.jumancuso.info - May i sex dating with you?
5 Homebrew Python Games

Johnathon Alston

September 7, 2010
hello great forum lots of helpful people just what i need can anybody tell me how they have built there list for people interested in marketing hopefully this is just ...
OOXML Vote Coverage

thrhtrurth

September 7, 2010
epvmqyauvloxitvmszkrv. http://www.acnetreatment2k.com/ - acne treatment npympx
Introducing Soturi - yet another Django blog application

nexium uk

September 7, 2010
Nexium Latest News: Nexium <a target="_blank" class="ext" href=http://mysoccerspace.ning.com/profiles/blogs/generic-nexium-generic-nexium>cheap generic nexium</a> Nexium <a target="_blank" class="ext" href=http://www.wfmtintroductions.com/profiles/blogs/buy-generic-nexium-buy-generic>buy nexium 40mg</a> Nexium <a target="_blank" class="ext" href=http://www.interdojo.com/profiles/blogs/buy-cheap-nexium-buy-cheap>nexium buy cancun</a> Nexium <a target="_blank" class="ext" href=http://www.translatorsgroup.com/profiles/blogs/nexium-generic-buy-nexium>nexium generic ...
SFTP in Python: Paramiko

payclickk

September 7, 2010
?????? ???????? ????????? ??? ???????????: http://pay-click.ru/?ref=3871 - http://pay-click.ru/?ref=3871
SFTP in Python: Really Simple SSH

Erotikalia

September 6, 2010
Ihr habt eine schoene Webseite hier, und vielciht schaut Ihr euch auchmal meine an, ok Sex im Internet ist nicht jedermans Sache, aber eben meine erste Homepage. Danke und macht ...
Introducing Soturi - yet another Django blog application

SonIntuivetut

September 5, 2010
I enjoyed reading your blog. Keep it that way.
The Pirate Bay Trial Begins

hgfllkj

September 4, 2010
?????????? ???? ?????????? ??? ???????????, ???? ?????????? ?????????, ?????????? ???? ??????????, ???? ???? ?????????, ?e?? ??a??????a ?e? ?e?????a???, ???? ?????????? ??? ???????????, ???? ?????????? ??? ???. ???? 24lux.ru
Include ODF support in the Linux Standard Base?

SemeRuppy

September 3, 2010
JACKELINE HERMINE you go, there's an KAREY
PyCon UK 2008 this September

neffeengard

September 3, 2010
LYN NOVELLA learned how to forgive those people and let it go, SHANON
PyCon UK 2008 this September

Essex Web Design

September 3, 2010
A lot of contract providers give you free internet usage now, but if you have Pay As You Go, then you are going to be paying heavy prices.
Calling time on mobile internet nonsense?

Krasochka

September 2, 2010
Hack again?!
Adding more terminals to your function keys

GenryFlorist

September 2, 2010
<b>Cheap flowers delivery around the world!</b> Celebrate summer with our gorgeous flowers. They?re the perfect gift for any summer occasion. From birthdays to anniversaries, we offer beautiful flowers, lush plants, ...
Burning an iso to CD on Windows

auto-financing.co.cc

September 2, 2010
auto-financing
ReStructuredText tables and doctests

rubaxa

September 1, 2010
FTP = NOT RANDOM software Dominated hands postflop suckout often on all-ins. EX. AK vs. A9 or KQ vs. K6. Both players hit top pair. Bad player goes all in ...
Burning an iso to CD on Windows

empodayaddelm

September 1, 2010
Sorry admin - my post is test
This Week: Heroes and Monsters