PEP308: Proposing function-call syntax
bokr at oz.net
Tue Feb 11 05:41:22 CET 2003
On Mon, 10 Feb 2003 10:44:41 +0100, "Anders J. Munch" <andersjm at dancontrol.dk> wrote:
>Yet-another PEP 308 proposal:
> ifelse(condition, iftrue, iffalse)
>That is, use plain old function call notation, but with 'ifelse' being
>a keyword recognised by the interpreter, so that arguments are only
>evaluated as necessary.
> messsage = "Access " + ifelse(key=correct_key, "granted", "denied")
I'd like to counter with my latest ;-)
messsage = "Access " + (key==correct_key -> "granted" -> "denied")
+ >Syntax is straightforward yet terse.
+ >Order of evaluation left-to-right.
+ >There's no confusing it with a statement.
? >'ifelse' is easy for newbies to search for in documentation.
+ not: >Looks like a function call but isn't. People might expect arguments
>to be fully evaluated.
+ not:>Adds new keyword.
>A possible variation: Call it 'cond' instead and allow for multiple
> cond(cond1, expr1, ..., condN, exprN, exprOtherwise)
Got that too:
cond1 -> expr1 >< condN -> exprN >< True -> exprOtherwise
and final ">< True" is optional, so
cond1 -> expr1 >< condN -> exprN -> exprOtherwise
and the basic ternary becomes
cond1 -> expr1 -> exprOtherwise
IOW, the syntax is
multiary ::= cond "->" expr ("><" cond "->" expr)* [ "->" expr]
where cond is an expr and its bool(expr) value is used to decide whether
to evaluate the associated expr after the "->". If "><" appears next, another
cond is evaluated, but if another "->" appears instead, the cond is treated
as True and the immediately following expr is evaluated.
More information about the Python-list