Add if C : e1 else e2.

This commit is contained in:
Guido van Rossum 2003-02-07 22:34:54 +00:00
parent 648a2a5acc
commit c486401240
1 changed files with 23 additions and 0 deletions

View File

@ -71,6 +71,7 @@ Alternatives
the question mark requires a matching colon); for people not used
to C-derived language, it is hard to understand.
---
David Ascher proposed, and Eric Raymond even implemented, a
variant that doesn't have this problem:
@ -81,6 +82,7 @@ Alternatives
arbitrary punctuation with an arbitrary meaning; and it's no
easier to understand than the ?: form.
---
If we could live with adding a new keyword, we could use:
@ -99,6 +101,27 @@ Alternatives
advantage of evaluating strictly from left to right (not that that
is a requirement for being Pythonic -- list comprehensions don't).
---
To deal with the problem of adding a new keyword, this variant has
been proposed:
if <condition> : <expression1> else <expression2>
This has the same ambiguity problem as the previous one (I would
even say more so), and lacks symmetry. It also begs the question
why there isn't a colon after the 'else'. But this:
if <condition> : <expression1> else: <expression2>
is even more confusing because it resembles the if statement so
much. (A solution that *doesn't* resemble the if statement is
better IMO since it should be obvious at first glance whether
we're dealing with an if expression or with an if statement.
Placing the 'if' in the middle somehow satisfies this
requirement.)
---
Many people suggest adding a new builtin instead of extending the
syntax of the language, e.g.: