For Python 2.5 and newer there is a specific syntax:
[on_true] if [cond] else [on_false]
In older Pythons a ternary operator is not implemented but it's possible to simulate it.
cond and on_true or on_false
Though, there is a potential problem, which if cond evaluates to True and on_true evaluates to False then on_false is returned instead of on_true. If you want this behavior the method is OK, otherwise use this:
{True: on_true, False: on_false}[cond is True] # is True, not == True
which can be wrapped by:
def q(cond, on_true, on_false)
return {True: on_true, False: on_false}[cond is True]
and used this way:
q(cond, on_true, on_false)
It is compatible with all Python versions.