(test) ? a:b

Marko Rauhamaa marko at pacujo.net
Thu Oct 23 09:44:06 EDT 2014


"BartC" <bc at freeuk.com>:

>> Ah, but what would
>>
>> x = [f, g][cond]()
>>
>> produce?
>
> It will select f or g (which should refer to functions), and call one of
> those depending on cond. That's not a problem.
>
> The problem is it will still evaluate both f and g,

That's not really the problem. The problem is in readability.

However, the "[f, g][cond]()" technique is how pure lambda calculus
implements conditional branching so it is interesting in its own right.
IOW, you can do "short-circuiting" in purely functional programming:

    j = j + 1 if j < 10 else 3

    <=>

    j = (lambda: 3, lambda: j + 1)[j < 10]()


Marko



More information about the Python-list mailing list