Python paradigms

David Goodger dgoodger at
Sat Apr 8 23:34:58 EDT 2000

on 2000-04-08 04:38, Nick Maclaren (nmm1 at wrote:
> x = (a != NULL ? a[i]->weeble : 0) + (b != NULL ? b[i]->wombat : 0)

C's ?: operator, ie:

    test ? true : false

can be done in Python, with:

    ((test and [true]) or [false])[0]

Note that I said *can* be done, not *should* be done! It is very ugly and
counter-intuitive and naughty. But sometimes necessary. Use sparingly,
especially if your code will ever be read (even by you!).

I got this expression from Mark Lutz' book "Programming Python", end of
Chapter 5, page 135 (under "The Naughty Bits", "if as an expression").

David Goodger    dgoodger at    Open-source projects:
 - The Go Tools Project:
 (more to come!)

More information about the Python-list mailing list