For review: PEP 308 - If-then-else expression
Michele Simionato
mis6 at pitt.edu
Sat Feb 8 08:55:13 EST 2003
from_deja at alandaniels.com (Alan Daniels) wrote in message news:<a6f9355c.0302072119.169349c9 at posting.google.com>...
> 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.
This is equivalent to add a new keyword/punctuation to python, in this case "?".
I would prefer
x= when y<5 "Yep" else "Nope"
or even (especially for use in list comprehension)
x="Yep" when y<5 else "Nope"
(proposed by Sean Ross). For some reason, my mind has some problem with "if",
that I associate to a statement, not to an expression.
Anyway, I am +1 on the conditional operator, but I would prefer a
new keyword/punctuation instead of if.
More information about the Python-list
mailing list