Proposal: function which simulates C ?: operator

Paul Rubin http
Sun Jul 11 13:57:00 EDT 2004

Adal Chiriliuc <me at> writes:
> I think a function similar to the one below should be added to the
> builtin module:
> def boolselect(condition, trueresult, falseresult):
>     if condition:
>         return trueresult
>     else:
>         return falseresult

That doesn't work because both results get evaluated either way.  E.g.

   boolselect(x==0, f(x), g(x))  

calls both f and g.  You need something like

  (lambda: g(x), lambda: f(x))[bool(condition)]()

More information about the Python-list mailing list