chasebook

blag/home playables hearables seeables about contact
chasebook
The famously neglected blog of Chase Shimmin!

Entries by category:
Sep. 2, 2009
Python Ternary Operator

Here's a cool way to emulate the ternary operator in python:

{True: true-return, False: false-return}[expression]

For instance, suppose you wanted to output a string indicating a number of items:

print "Found %d item%s." % (numitems, \
{True: '', False: 's'}[numitems == 1])