Things That Are Wrong with Python (1): True and False

February 12th, 2009 | by Will |

The following values are all false in Python programming language.

  1. None
  2. False
  3. numbers equal to 0: 0, 0L, 0.0, 0j
  4. empty sequences:e.g., ‘”", (), [], array.array(’i')
  5. empty maps: e.g., {}
  6. any object that defines a __nonzero__ or __len__ method (returning False or len=0, respectively

This is a Thing that is Wrong with Python.

(The Right Way: false is None or False; everything else is true.)

(Note: I really do disagree with this design decision, but I’m mostly writing this down so I will remember not to write code like this:


if object.table: print 'table was initialized'

but instead remember to write:

if object.table and len(object.table)>0: print 'table was initialized'

Sorry, comments for this entry are closed at this time.