Is there something similar to ?: operator (C/C++) in Python?

Antoon Pardon apardon at
Thu Jun 30 10:39:57 CEST 2005

Op 2005-06-29, Scott David Daniels schreef <Scott.Daniels at Acm.Org>:
> Roy Smith wrote:
>> Andrew Durdin <adurdin at> wrote:
>>>Corrected version:
>>>    result = [(lambda: expr0), lambda: expr1][bool(cond)]()
> Sorry, I thought cond was a standard boolean.
> Better is:
>      result = [(lambda: true_expr), lambda: false_expr][not cond]()

How about the following:

  result = (cond and (lambda: true_expr) or (lambda: false_expr))()

Antoon Pardon

More information about the Python-list mailing list