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

Chris Tavares christophertavares at earthlink.net
Mon Oct 15 02:16:12 EDT 2001


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.1003089072.26009.python-list at python.org...
> [Tim]
[... snip ... ]
> 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
>
[ ... snip ... ]
> 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.
>

May I be the first to say - ICK!

I have enough political problems trying to get people I know to try python
out - adding a wart like this ( and requiring parens on this one, and only
one expression is really a wart!) and trying to explain it to newbies
doesn't strike me as a winner, and I've never really pined for this as an
experienced user.

Question - what happens with:

x = (if e1 then e2 else e3,)  # <--- note trailing comma

Should be a 1 element tuple, yes? What happens? Or would you have to write:

x = ((if e1 then e2 else e3),)

That's just ugly.

-Chris






More information about the Python-list mailing list