PEP 274 has been updated to reflect current reality.

This commit is contained in:
Barry Warsaw 2012-04-09 10:42:15 -04:00
parent 954c3684ce
commit 1edb8f9940
1 changed files with 21 additions and 41 deletions

View File

@ -2,11 +2,11 @@ PEP: 274
Title: Dict Comprehensions
Version: $Revision$
Last-Modified: $Date$
Author: barry@python.org (Barry Warsaw)
Status: Withdrawn
Author: Barry Warsaw <barry@python.org>
Status: Accepted
Type: Standards Track
Created: 25-Oct-2001
Python-Version: 2.3
Python-Version: 2.7, 3.0 (originally 2.3)
Post-History: 29-Oct-2001
@ -19,13 +19,20 @@ Abstract
very similar to list comprehensions, except that they produce
Python dictionary objects instead of list objects.
Resolution
This PEP is withdrawn. Substantially all of its benefits were
subsumed by generator expressions coupled with the dict() constructor.
This PEP was originally written for inclusion in Python 2.3. It
was withdrawn after observation that substantially all of its
benefits were subsumed by generator expressions coupled with the
dict() constructor.
However, Python 3.0 introduces this exact feature, as well as the
closely related set comprehensions.
However, Python 2.7 and 3.0 introduces this exact feature, as well
as the closely related set comprehensions. On 2012-04-09, the PEP
was changed to reflect this reality by updating its Status to
Accepted, and updating the Python-Version field. The Open
Questions section was also removed since these have been long
resolved by the current implementation.
Proposed Solution
@ -33,10 +40,9 @@ Proposed Solution
Dict comprehensions are just like list comprehensions, except that
you group the expression using curly braces instead of square
braces. Also, the left part before the `for' keyword expresses
both a key and a value, separated by a colon. (There is an
optional part of this PEP that allows you to use a shortcut to
express just the value.) The notation is specifically designed to
remind you of list comprehensions as applied to dictionaries.
both a key and a value, separated by a colon. The notation is
specifically designed to remind you of list comprehensions as
applied to dictionaries.
Rationale
@ -65,7 +71,7 @@ Semantics
The semantics of dict comprehensions can actually be demonstrated
in stock Python 2.2, by passing a list comprehension to the
builtin dictionary constructor:
built-in dictionary constructor:
>>> dict([(i, chr(65+i)) for i in range(4)])
@ -73,7 +79,7 @@ Semantics
>>> {i : chr(65+i) for i in range(4)}
The dictionary constructor approach has two dictinct disadvantages
The dictionary constructor approach has two distinct disadvantages
from the proposed syntax though. First, it isn't as legible as a
dict comprehension. Second, it forces the programmer to create an
in-core list object first, which could be expensive.
@ -104,36 +110,10 @@ Examples
2, 3): 5}
Open Issues
- There is one further shortcut we could adopt. Suppose we wanted
to create a set of items, such as in the "list_of_email_addrs"
example above. Here, we're simply taking the target of the for
loop and turning that into the key for the dict comprehension.
The assertion is that this would be a common idiom, so the
shortcut below allows for an easy spelling of it, by allow us to
omit the "key :" part of the left hand clause:
>>> print {1 for x in list_of_email_addrs}
{'barry@zope.com' : 1, 'barry@python.org' : 1, 'guido@python.org' : 1}
Or say we wanted to map email addresses to the MX record handling
their mail:
>>> print {mx_for_addr(x) for x in list_of_email_addrs}
{'barry@zope.com' : 'mail.zope.com',
'barry@python.org' : 'mail.python.org,
'guido@python.org' : 'mail.python.org,
}
Questions: what about nested loops? Where does the key come
from? The shortcut probably doesn't save much typing, and comes
at the expense of legibility, so it's of dubious value.
Implementation
TBD
All implementation details were resolved in the Python 2.7 and 3.0
time-frame.
References