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

Omar Zabaneh

July 25, 2008
Zeth, Thank you for this post, very helpful. I used it as a basis for my own email validation function that i wish to share with you, in a selfish ...
Email Syntax Check in Python

Double Booting Bastard

July 24, 2008
I agree with Nui, Linux is great for many things but not everything. A lot of, less mainstream, hardware is a time consuming and often fruitless task to install and ...
Give Linux a chance

John

July 23, 2008
Duncan, sadly the permissions are stored with the data (inode), not with the directory entries (hard-links). Zeth needs ACLs -- no way to do this with basic unix permissions.
Advanced Unix Groups

Garrick

July 21, 2008
I do love my iPhone. That being said, I would trade it in a heartbeat for a STABLE Openmoko FreeRunner.
This week - iPhone vs a can of compressed air, and Django NewFormsAdmin

Daniel Davies

July 21, 2008
With regards to your last paragraph, you are certainly correct. Right now Django is a nightmare to use across multiple sites... we have some sites running the newformsadmin branch, others ...
This week - iPhone vs a can of compressed air, and Django NewFormsAdmin

Nui

July 18, 2008
Hmm, this would be more persuasive as an argument with some evidence. I am a happy admin of Windows and a novice user of Linux, so I have taken the ...
Give Linux a chance

Paddy3118

July 18, 2008
Hi, I too work with Electronic Design Automation tools, where Tcl is used extensively. I tend to only occasionally have to write in Tcl and so find the TclTutor utility: ...
Python and TCL

Cliff Wells

July 17, 2008
I personally cannot live without the Web Developer extension or Firebug. Unfortunately these are probably both among the more difficult to port extensions. Given how poorly Firefox functions on Linux ...
Will Epiphany be able to compete with Firefox's extensions?

Åke Forslund

July 13, 2008
I'm pretty much a novice in both of these languages but I find them both easy to use and preform the tasks I give them. However I rarely use them ...
Python and TCL

Christopher Thoday

July 12, 2008
A single test is not sufficient to give you confidence that the algorithm is working. You should make 'number' an argument of 'main' so that you can test some boundary ...
Python and TCL

paul21

July 10, 2008
Shame on Mozilla. They should make developers specify the extension license before hosting it. They should show the license next to download button as well.
Are your Firefox extensions proprietary software?

Tris

July 8, 2008
Justin - You say they had not heard of Linux? That doesn't sound very professional to me!
Give Linux a chance

michael

July 8, 2008
what about Galeon? in Gnome i use Galeon mostly. it is fast and stable and has a nice portal with search masks for Debian, FSF, Freshmeat and so on. wtf ...
Will Epiphany be able to compete with Firefox's extensions?

vermin

July 7, 2008
> Eventually, after a bit of digging and Googling, I found their Toolbar-License... You simply found the license of the StumbleUpon Toolbar for Internet Explorer. This is another product, much ...
Are your Firefox extensions proprietary software?

Andrew West

July 6, 2008
Both the Python and the Tcl example could do with error checking. While at first this may not seem on topic with the post I think it better shows the ...
Python and TCL