PEP 308 - ternary operator
Tim Rowe
tim at remove_if_not_spam.digitig.co.uk
Fri Feb 21 11:12:19 EST 2003
Erik Max Francis wrote:
> Nick Trout wrote:
>
>
>>Has anyone pointed out that your can do ternary operators using:
>>
>>result = { 1: true_value, 0: false_value }[condition]
>>
>>or
>>
>>result = { 1: true_fn, 0: false_fn }[condition]()
>>
>>if they are functions.
>
>
> This doesn't support short-circuiting, which is the key feature of a
> true conditional operator. The PEP itself goes into great detail about
> precisely what this feature is and why it's necessary.
>
Surely the true pythonic way would be
result = eval("true_fn", "false_fn")[not(condition)]
Does short-circuit, avoids the issue in Nick's version that "true" may
not be 1, as easy to read as at least /some/ of the PEP 308 proposals...
I really don't see what all the fuss is about! :-)
More information about the Python-list
mailing list