PEP 308 is final. A great many thanks go to Thomas Wouters!

This commit is contained in:
Guido van Rossum 2006-02-27 16:55:05 +00:00
parent 3e2085f82e
commit df7a33afb0
2 changed files with 15 additions and 9 deletions

View File

@ -65,7 +65,6 @@ Index by Category
Accepted PEPs (accepted; may not be implemented yet) Accepted PEPs (accepted; may not be implemented yet)
SA 308 Conditional Expressions GvR, Hettinger
SA 328 Imports: Multi-Line and Absolute/Relative Aahz SA 328 Imports: Multi-Line and Absolute/Relative Aahz
SA 343 The "with" Statement GvR, Coghlan SA 343 The "with" Statement GvR, Coghlan
SA 352 Required Superclass for Exceptions GvR, Cannon SA 352 Required Superclass for Exceptions GvR, Cannon
@ -158,6 +157,7 @@ Index by Category
SF 301 Package Index and Metadata for Distutils Jones SF 301 Package Index and Metadata for Distutils Jones
SF 305 CSV File API Montanaro, et al SF 305 CSV File API Montanaro, et al
SF 307 Extensions to the pickle protocol GvR, Peters SF 307 Extensions to the pickle protocol GvR, Peters
SF 308 Conditional Expressions GvR, Hettinger
SF 309 Partial Function Application Harris SF 309 Partial Function Application Harris
SF 311 Simplified GIL Acquisition for Extensions Hammond SF 311 Simplified GIL Acquisition for Extensions Hammond
SF 318 Decorators for Functions and Methods Smith, et al SF 318 Decorators for Functions and Methods Smith, et al
@ -361,7 +361,7 @@ Numerical Index
SF 305 CSV File API Montanaro, et al SF 305 CSV File API Montanaro, et al
I 306 How to Change Python's Grammar Hudson I 306 How to Change Python's Grammar Hudson
SF 307 Extensions to the pickle protocol GvR, Peters SF 307 Extensions to the pickle protocol GvR, Peters
SA 308 Conditional Expressions GvR, Hettinger SF 308 Conditional Expressions GvR, Hettinger
SF 309 Partial Function Application Harris SF 309 Partial Function Application Harris
SR 310 Reliable Acquisition/Release Pairs Hudson, Moore SR 310 Reliable Acquisition/Release Pairs Hudson, Moore
SF 311 Simplified GIL Acquisition for Extensions Hammond SF 311 Simplified GIL Acquisition for Extensions Hammond

View File

@ -3,7 +3,7 @@ Title: Conditional Expressions
Version: $Revision$ Version: $Revision$
Last-Modified: $Date$ Last-Modified: $Date$
Author: Guido van Rossum, Raymond D. Hettinger Author: Guido van Rossum, Raymond D. Hettinger
Status: Accepted Status: Final
Type: Standards Track Type: Standards Track
Content-Type: text/plain Content-Type: text/plain
Created: 7-Feb-2003 Created: 7-Feb-2003
@ -37,7 +37,7 @@ Adding a conditional expression
... ...
gen_for: 'for' exprlist 'in' or_test [gen_iter] gen_for: 'for' exprlist 'in' or_test [gen_iter]
The new syntax introduces a minor syntactical backwards The new syntax nearly introduced a minor syntactical backwards
incompatibility. In previous Python versions, the following is incompatibility. In previous Python versions, the following is
legal: legal:
@ -46,8 +46,8 @@ Adding a conditional expression
(I.e. a list comprehension where the sequence following 'in' is an (I.e. a list comprehension where the sequence following 'in' is an
unparenthesized series of lambdas -- or just one lambda, even.) unparenthesized series of lambdas -- or just one lambda, even.)
In Python 2.5, the series of lambdas will have to be In Python 3.0, the series of lambdas will have to be
parenthesized: parenthesized, e.g.:
[f for f in (lambda x: x, lambda x: x**2) if f(1) == 1] [f for f in (lambda x: x, lambda x: x**2) if f(1) == 1]
@ -56,9 +56,15 @@ Adding a conditional expression
followed by an 'if' keyword that binds less tightly still (for followed by an 'if' keyword that binds less tightly still (for
details, consider the grammar changes shown above). details, consider the grammar changes shown above).
Given that this is a rather odd corner of the syntax the BDFL does However, in Python 2.5, a slightly different grammar is used that
not believe this backwards incompatibility requires us to use a is more backwards compatible, but constrains the grammar of a
future statement to enable the new syntax. lambda used in this position by forbidding the lambda's body to
contain an unparenthesized condition expression. Examples:
[f for f in (1, lambda x: x if x >= 0 else -1)] # OK
[f for f in 1, (lambda x: x if x >= 0 else -1)] # OK
[f for f in 1, lambda x: (x if x >= 0 else -1)] # OK
[f for f in 1, lambda x: x if x >= 0 else -1] # INVALID
References References