For review: PEP 308 - If-then-else expression

Alan Daniels from_deja at alandaniels.com
Sat Feb 8 00:19:15 EST 2003


Guido van Rossum <guido at python.org> wrote in message news:<mailman.1044638179.4983.python-list at python.org>...

[snippage...]
> Many C-derived languages use this syntax:
> 
>  <condition> ? <expression1> : <expression2>
> 
>I reject this for several reasons: the colon already has many uses
>in Python (even though it would actually not be ambiguous, because
>the question mark requires a matching colon)...

If use of the colon character is overloaded, how about just using the
"else" keyword instead? That's what the colon in C's ternary operator
essentially means anyway. So, we'd have something like:

  <condition> ? <expression1> else <expression2>

A small compromise between the C-syntax and a more Pythonish syntax.
I would find something like

  x = (y < 5) ? "Yep" else "Nope"

pretty easy to read, and I think this would be the case for people both
with and without backgrounds in C. Also, I can't think of any ambiguity
with existing use of the "else" keyword, since it is currently always
used on a single line by itself anyway. Just an idea.




More information about the Python-list mailing list