Will.Whim

A weblog by Will Fitzgerald

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

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'

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: