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

Erik Max Francis max at alcyone.com
Sat Feb 8 12:50:22 EST 2003


Michele Simionato wrote:

> +1 to "when". To me "if" is associated to the conditional statement, a
> conditional expression is a conceptually  different thing and needs a
> different expression. Readability is worth adding a new keyword.

If we're going to add another keyboard, I'd prefer the full

	if p then x else y

syntax, perhaps with required parenthesization (I'm ambivalent on that
latter point).  So far only two objections have struct any kind of chord
with me, despite being staunchly in favor of a conditional operator, in
whatever form I can get it:

1.  The proposed syntax overrides the meaning of `if';

2.  The proposed x if p else y syntax reads neither left-to-right nor
right-to-left.

The first seems to me a bit of a red herring, but I see the point.  The
replacement if `if' with `when' does indeed address this concern, but it
does seem rather a secondary objection, since the use of `if' is already
overloaded, e.g., in list comprehensions.  (So instead of it polluting
our precious `if', there's already precedent.)

The second objection is really the only one I can sympathize with, which
is why I'm not 100% for the proposal as it stands (even though I'm 100%
for the addition of _some_ form of a conditional operator; I'll takes
what I can gets).

In pure name collision likelihood, it seems more likely that people will
have preexisting code using the name `when' rather than code using the
name 'then'.  So it seems to me that the real clincher is whether or not
you want to let in a new keyword, then I'd say go with `then' and admit
the `if p then x else y' syntax, killing two birds with one stone.

> P.S. I don't like your other proposal with the comma, my mind parses
> when C,x as the tuple when (C,x).

Agreed.  Whatever the syntax, the three expressions should be separated
by something concrete, and not a comma.  The `when p, x else y' and,
worse yet, `when p x else y' syntaxes are too confusing.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Men live by forgetting -- women live on memories.
\__/ T.S. Eliot
    REALpolitik / http://www.realpolitik.com/
 Get your own customized newsfeed online in realtime ... for free!




More information about the Python-list mailing list