Python paradigms

David Goodger dgoodger at bigfoot.com
Sun Apr 9 05:34:58 CEST 2000


on 2000-04-08 04:38, Nick Maclaren (nmm1 at cus.cam.ac.uk) 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 bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list