Add if C : e1 else e2.
This commit is contained in:
parent
648a2a5acc
commit
c486401240
23
pep-0308.txt
23
pep-0308.txt
|
@ -71,6 +71,7 @@ Alternatives
|
||||||
the question mark requires a matching colon); for people not used
|
the question mark requires a matching colon); for people not used
|
||||||
to C-derived language, it is hard to understand.
|
to C-derived language, it is hard to understand.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
David Ascher proposed, and Eric Raymond even implemented, a
|
David Ascher proposed, and Eric Raymond even implemented, a
|
||||||
variant that doesn't have this problem:
|
variant that doesn't have this problem:
|
||||||
|
@ -81,6 +82,7 @@ Alternatives
|
||||||
arbitrary punctuation with an arbitrary meaning; and it's no
|
arbitrary punctuation with an arbitrary meaning; and it's no
|
||||||
easier to understand than the ?: form.
|
easier to understand than the ?: form.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
If we could live with adding a new keyword, we could use:
|
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
|
advantage of evaluating strictly from left to right (not that that
|
||||||
is a requirement for being Pythonic -- list comprehensions don't).
|
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
|
Many people suggest adding a new builtin instead of extending the
|
||||||
syntax of the language, e.g.:
|
syntax of the language, e.g.:
|
||||||
|
|
Loading…
Reference in New Issue