For review: PEP 308 - If-then-else expression
Paul Rubin
phr-n2003b at NOSPAMnightsong.com
Sat Feb 8 19:39:30 EST 2003
"Paul Paterson" <hamonlypaulpaterson at houston.rr.com> writes:
> However, for me this is the crux of the matter. Short circuiting pretty much
> has to be the justification for implementing this PEP because if you don't
> need short-circuiting you can write a trivial three line iff function. But
> then if short-circuiting is the main reason then I think I'd rather open up
> the general idea of how to implement lazy evaulation in a Pythonic way.
How about further abuse of colon:
f(:expression)
is short for f(lambda: expression)
So we'd implement a conditional expression with
def cond(condition, v1, v2):
if condition:
return v1()
else:
return v2()
To call it, we'd say, for example,
print cond(x >= 0, :sqrt(x), :"imaginary roots")
I don't see immediately whether this makes any syntax ambiguities with
existing statements, since I can't think of anyplace where colon can
currently appear where an expression can also appear.
More information about the Python-list
mailing list