conditional expressions (RE: Loop-and-a-half (Re: Curious assignment behaviour))

Ian Parker parker at gol.com
Mon Oct 15 09:33:02 CEST 2001


In article <mailman.1003089072.26009.python-list at python.org>, Tim Peters
<tim.one at home.com> writes
>[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
>
>

Presumably this wouldn't lead to any misleading syntax error
identification problems with multiple lines?  For example multi-line
versions of these new 'if' expressions, or where a trailing '{' has 
been left at the end of a line preceding a normal 'if' statement.

Would sensible syntax errors and line numbers still be reported?   

Not that I leave many trailing "("s. but if I were to do so, I'd like to
have as much help as possible tracking them down.
-- 
Ian Parker



More information about the Python-list mailing list