Proposal: function which simulates C ?: operator

Paul Rubin http
Sun Jul 11 22:58:53 CEST 2004

Tor Iver Wilhelmsen <tor.iver.wilhelmsen at> writes:
> >   (lambda: g(x), lambda: f(x))[bool(condition)]()
> Since you call the object afterwards, you don't need the lambdas
> (which add a layer of indirection), just the function references:
> (g, f)[bool(condition)]()

Good catch, for that particular example.  The lambdas are needed for
more general cases, e.g. (x > 0 ? (f(x)+g(y)) : (h(x)-z(y)))

There's been various proposals floated for macros in Python.  If
something like that becomes real, that can solve this whole problem.

More information about the Python-list mailing list