[Python-Dev] conditional expressions (RE: Loop-and-a-half (Re: Curious assignment behaviour))
Tim Peters
tim.one@home.com
Sun, 14 Oct 2001 15:50:36 -0400
[Tim]
> If people sign off on taking "then" as a new keyword, I think
> the chances are good that we could get
>
> x = if e1 then e2 else e3
>
> into 2.2b1. That's the only obvious spelling, hence the only
> truly Pythonic way to spell it. Other languages spelling it that
> way range from Algol-60 (Guido's first intense language affair) to
> Haskell.
[Paul Rubin, among others of similar mind]
> This sounds fine to me.
Alas, it didn't to Python's parser -- one-token lookahead isn't enough to
distinguish
if 1:
from
if 1 then 2 else 3
let alone
if a + b / c:
from
if a + b / c then 2 else 3
etc.
and Python won't grow anything a simple parser can't sort out.
Everything's cool if parens are required around a conditional expression,
though, in which case:
x = if e1 then e2 else e3 + 1 # SyntaxError
x = (if e1 then e2 else e3) + 1 # cool
x = (if e1 then e2 else e3 + 1) # cool
x = if e1 then e2 else e3 # SyntaxError
x = (if e1 then e2 else e3) # cool
x = if if e1 then e2 else e3 then e4 else e5 # SyntaxError
x = (if (if e1 then e2 else e3) then e4 else e5) # cool
Seems a mixed bag, but I'm more interested in readability and the
functionality than in minimizing keystrokes; requiring parens doesn't hurt
the goals I care about.
implemented-but-not-checked-in-ly y'rs - tim