Clarify the new syntax and note a backwards incompatibility.
This commit is contained in:
parent
78e4ec3a47
commit
3eed115841
33
pep-0308.txt
33
pep-0308.txt
|
@ -27,6 +27,39 @@ Adding a conditional expression
|
|||
sampling of real-world use cases, across a variety of applications,
|
||||
written by a number of programmers with diverse backgrounds). [3]
|
||||
|
||||
The following change will be made to the grammar. (The or_test
|
||||
symbols is new, the others are modified.)
|
||||
|
||||
test: or_test ['if' or_test 'else' test] | lambdef
|
||||
or_test: and_test ('or' and_test)*
|
||||
...
|
||||
testlist_safe: or_test [(',' or_test)+ [',']]
|
||||
...
|
||||
gen_for: 'for' exprlist 'in' or_test [gen_iter]
|
||||
|
||||
The new syntax introduces a minor syntactical backwards
|
||||
incompatibility. In previous Python versions, the following is
|
||||
legal:
|
||||
|
||||
[f for f in lambda x: x, lambda x: x**2 if f(1) == 1]
|
||||
|
||||
(I.e. a list comprehension where the sequence following 'in' is an
|
||||
unparenthesized series of lambdas -- or just one lambda, even.)
|
||||
|
||||
In Python 2.5, the series of lambdas will have to be
|
||||
parenthesized:
|
||||
|
||||
[f for f in (lambda x: x, lambda x: x**2) if f(1) == 1]
|
||||
|
||||
This is because lambda binds less tight than the if-else
|
||||
expression, but in this context, the lambda could already be
|
||||
followed by an 'if' keyword that binds less tightly still (for
|
||||
details, consider the grammar changes shown above).
|
||||
|
||||
Given that this is a rather odd corner of the syntax the BDFL does
|
||||
not believe this backwards incompatibility requires us to use a
|
||||
future statement to enable the new syntax.
|
||||
|
||||
|
||||
References
|
||||
|
||||
|
|
Loading…
Reference in New Issue