For review: PEP 308 - If-then-else expression
Paul Moore
gustav at morpheus.demon.co.uk
Fri Feb 7 16:05:27 EST 2003
Andrew Koenig <ark at research.att.com> writes:
> Harvey> What about a built-in function?
>
> Harvey> iff(<condition>, <trueresult>, falseresult) which returns
> Harvey> <trueresult> if <condition> is true, otherwise returns
> Harvey> <falseresult
>
> If it were a function, it would have to evaluate all of its arguments.
> Part of the point is that only one of the two results is evaluated.
Interestingly (maybe), your example from another post
strs = [("?" if i < 0 else str(i)) for i in ints]
didn't need only one of the results to be evaluated.
I'd be interested to know how many real cases would actually rely on
short-circuit evaluation. And those that don't can quite happily be
handled with
def iff(cond, trueval, falseval):
if cond:
return trueval
else:
return falseval
(oh, and this function is short enough, and I expect its use to be
rare enough, that I see no need for it to be builtin).
But I do agree that if anything justifies the need for conditional
expressions, it's use in lambda and list comprehensions.
Personally, I don't see enough of a need to be worth it, though.
Paul.
--
This signature intentionally left blank
More information about the Python-list
mailing list