What is truth? - part 1 - false vs zero

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.

One thought on “What is truth? - part 1 - false vs zero”

  1. <p>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.</p>

How about Global Thermonuclear War? Wouldn't you prefer a good game of chess? Powered by zpress