Proposed PEP for a Conditional Expression
Michael Chermside
mcherm at destiny.com
Fri Sep 14 16:24:16 EDT 2001
Markus> What about "(<cond>: return <expr> [else return <expr])"
Skip> All you're saving is a single word and a colon:
Skip>
Skip> if <cond>: return <expr>
Skip> else: return <expr>
Skip>
No, he's actually proposing a major change to the interpretation of
"return" in Python -- which is why I don't particularly like this syntax
(but perhaps this objection can be patched up!).
For instance, consider the following (written using the and-or trick):
def f(a,b=1):
x = a > 0 and a or b
return x ** 2 - 1
Now, if we tried to re-write this in Markus's syntax, we'd get the
following:
def f(a,b=1):
x = a > 0: return a else return b
return x ** 2 - 1
Apparently Markus's syntax implies that after a colon appearing in a
statement (as opposed to other, normal :'s in Python), the "return"
keyword no longer means to return from a function.
Markus... did I mis-interpret? Do you still think this is a useful syntax?
-- Michael Chermside
More information about the Python-list
mailing list