[Python-Dev] conditional expressions? (original) (raw)
Daniel Mahler mahler@cyc.com
Tue, 16 Oct 2001 02:51:32 -0500
- Previous message: [Python-Dev] conditional expressions?
- Next message: [Python-Dev] conditional expressions?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Tim Peters writes:
[Daniel Mahler]
Has an if(cond,exp1,exp2) syntax been considered?
Not seriously, no. That looks like a function call, and function arguments are always evaluated in Python, and that works against seeing this as a short-circuiting construct:
q = if y then x/y else copysign(INFINITY, x)
I do not see a problem: almost all of the infix operators also use eager evaluation, but 2 use lazy evaluation, so operator syntax by itself does not imply or even suggest nonstandard evaluation order and there is no systematic syntactic convention to indicate it. You just have to remember that "and" and "or" are the only lazy operators (you might count lambda) out of ~35 (by my count). -- anyway, I had to check my reference card to see they were the only ones. I do not see why this is any more problematic for functional syntax. Since there is no generic way of indicating lazyness at the expression level (":" kind of indicates it at the statement level), people will just have to remember that conditionals are special no matter which syntax is chosen.
OTOH conditionals are like functions in the syntactic sense (they take expressions as arguments and can be used anywhere expressions can) and in the mathematical sense (in the absence of side effects in the argument expressions the result is a mathematical function of the arguments). In lazy languages (Haskell,Miranda,Gofer) "if" is just a regular function.
Hmmm, now that I think about it, I wonder why python makes a strong distinction between statements and expressions, but does not distinguish between procedures and functions?
It would have the advantage that existing syntax sensitive editors would handle without change.
I don't see any problem there (and am responsible for two context sensitive Python edit gimmicks, and used to be responsible for a third -- all I expect to need to do is get "then" at token level colored as keyword).
My editor does more than highlighting. What about autoindentation if you split the line? Will one need a trailing "" if when splitting across lines? -- with the if/then/else syntax, we will be splitting lines in expressions. [and deep down inside python does not really like that ;)]. I actually prefer to split even concise syntax, for readability, and python is friendlier to splitting functional expressions than to splitting operator expressions.
Functional syntax would do the "right thing" automatically, even when nested, because it does not involve changing the syntax of the language. It just introduces lazy/call-by-name semantics. [Guido, can we have user defineable call-by-name functions -- pleeeease ;)]
Actually, I lied little -- nested if(,,_) confuses emacs python-mode, but change the name to, say, ifTE (short for ifThenElse) and it autoindents beautyfully
x = ifTE(a, ifTE(b, x, y), ifTE(c, w, z) )
I think conditional exprs badly needed, but the if/then/else syntax seems too verbose for use inside expressions.
Good -- then you won't be tempted to abuse it <0.5 wink>.
But one should be tempted to use them, otherwise one can just drop the feature, preventing abuse entirely. Note that "z = if a then x else y" is 2/3 syntactic sugar. I can see that "z = a ? x : y" is somewhat cryptic but no more than "x >> y", "x % y" or "x ^ y" ("^" is exponentiation in most other languages).
Of course, "z = if(a,x,y)" is just ... ;).
Daniel
best-in-small-doses-ly y'rs - tim
Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev
- Previous message: [Python-Dev] conditional expressions?
- Next message: [Python-Dev] conditional expressions?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]