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

Cupcake

July 31, 2010
Good post! You helped me a lot with my school project! CountryField(blank = True) < (K)
Countries in Django

LeshaShampoo

July 30, 2010
it was very interesting to read commandline.org.uk I want to quote your post in my blog. It can? And you et an account on Twitter?
Email Syntax Check in Python

vemma2018

July 30, 2010
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
On Comment Spam

layecenda

July 30, 2010
Hello. And Bye.test :) http://idfjhvihdfiphvlajbvhalibv.com
PuTTY Series: Adding PuTTY to your system path

scuba

July 30, 2010
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.
On Comment Spam

Businesking

July 30, 2010
Great site and articles for hack for win, I said Amazing post
How not to program WSGI

Tehnoking

July 30, 2010
This is Great post to learn about the hack Thumbs-up for you :D
How not to program WSGI

Syabiltech

July 30, 2010
I think this articles for master...because very hard to learning, As blogger beginners like me.
How not to program WSGI

coffeeatea

July 30, 2010
Are you looking for coffee gifts? We can tell you more about the coffee gifts including coffee machines and coffee pods.
Introducing Soturi - yet another Django blog application

noni juice

July 30, 2010
I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.
On Comment Spam

Dion Moult

July 29, 2010
What I do know is that ever since I tried out Opera and put their tab bar on the left as a column, I've loved that layout. Back on Firefox ...
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

ZonaEntertainment

July 29, 2010
Wow useful articles, I'm read to learn about this and now I bookmark this to my Facebook, thanks for share!
How not to program WSGI

Giacomo

July 29, 2010
Honestly, I think both Mozilla and you are wrong :) This sort of concept adds overhead. A user would have to manage all this crap, constantly dragging and dropping, creating ...
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

Matija "hook" Šuklje

July 29, 2010
As a minimalist, you'll probybly moan if I mention KDE, but I'll do so anyway ;) The future I want (and actually see slowly fold out before me) is to ...
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

tahitian noni

July 28, 2010
Thank You For This Blog, was added to my bookmarks.
On Comment Spam

Rick

July 28, 2010
I already have piles. It's called A New Window.
We need a thoughout integration of the desktop and the web - not Tab Candy superfast jellyfish

Tech News

July 25, 2010
Thanks for this short tutorial...was auto-FTPing my files from my appserver to webserver for my tech news website. Everything was OK until someone hacked it. Hosting provider is now recommending ...
SFTP in Python: Really Simple SSH

naypalm

July 24, 2010
During the past 3-4 years, I and many others have enjoyed unlimited 2G/3G internet. But ever since the massive cult-like following of i Phone users in the US, most cellular ...
Calling time on mobile internet nonsense?

Steve

July 15, 2010
Very occasionally, you will run into a Java program that uses a lot of memory just to hold all the classes used. It turns out that the JVM uses a ...
Three classic command line tips

no

July 14, 2010
1. number one 2. number two 4. number four 3. number three 6. number six # first # second ## second-ay ## second-bee ### second-bee-one ### second-bee-two
An Introduction to ReStructuredText