Some rearrangements and fixes based on recent comments.
This commit is contained in:
parent
4574958961
commit
e678d918f3
59
pep-0274.txt
59
pep-0274.txt
|
@ -35,10 +35,9 @@ Rationale
|
|||
|
||||
There are times when you have some data arranged as a sequences of
|
||||
length-2 sequences, and you want to turn that into a dictionary.
|
||||
In Python 2.2, the dictionary() constructor will take an optional
|
||||
keyword argument that indicates specifically to interpret a
|
||||
sequences of length-2 sequences as key/value pairs, and turn them
|
||||
into a dictionary.
|
||||
In Python 2.2, the dictionary() constructor accepts an argument
|
||||
that is a sequence of length-2 sequences, used as (key, value)
|
||||
pairs to initialize a new dictionary object.
|
||||
|
||||
However, the act of turning some data into a sequence of length-2
|
||||
sequences can be inconvenient or inefficient from a memory or
|
||||
|
@ -54,6 +53,24 @@ Rationale
|
|||
for loop.
|
||||
|
||||
|
||||
Semantics
|
||||
|
||||
The semantics of dictionary comprehensions can actually be
|
||||
demonstrated in stock Python 2.2, by passing a list comprehension
|
||||
to the builtin dictionary constructor:
|
||||
|
||||
>>> dictionary([(i, chr(65+i)) for i in range(4)])
|
||||
|
||||
is semantically equivalent to
|
||||
|
||||
>>> {i : chr(65+i) for i in range(4)}
|
||||
|
||||
The dictionary constructor approach has two dictinct 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.
|
||||
|
||||
|
||||
Examples
|
||||
|
||||
>>> print {i : chr(65+i) for i in range(4)}
|
||||
|
@ -72,6 +89,12 @@ Examples
|
|||
>>> print invert(d)
|
||||
{'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3}
|
||||
|
||||
>>> {(k, v): k+v for k in range(4) for v in range(4)}
|
||||
... {(3, 3): 6, (3, 2): 5, (3, 1): 4, (0, 1): 1, (2, 1): 3,
|
||||
(0, 2): 2, (3, 0): 3, (0, 3): 3, (1, 1): 2, (1, 0): 1,
|
||||
(0, 0): 0, (1, 2): 3, (2, 0): 2, (1, 3): 4, (2, 2): 4, (
|
||||
2, 3): 5}
|
||||
|
||||
|
||||
Open Issues
|
||||
|
||||
|
@ -99,36 +122,10 @@ Open Issues
|
|||
from? The shortcut probably doesn't save much typing, and comes
|
||||
at the expense of legibility, so it's of dubious value.
|
||||
|
||||
- Should nested for loops be allowed? The following example,
|
||||
taken from an earlier revision of this PEP illustrates the
|
||||
problem:
|
||||
|
||||
>>> print {k, v for k in range(4) for v in range(-4, 0, 1)}
|
||||
|
||||
The intent of this example was to produce a mapping from a
|
||||
number to its negative, but this code doesn't work because -- as
|
||||
in list comprehensions -- the for loops are nested, not in
|
||||
parallel! So the value of this expression is actually
|
||||
|
||||
{0: -1, 1: -1, 2: -1, 3: -1}
|
||||
|
||||
which seems of dubious value. For symmetry with list
|
||||
comprehensions, perhaps this should be allowed, but it might be
|
||||
better to disallow this syntax.
|
||||
|
||||
|
||||
Implementation
|
||||
|
||||
The semantics of dictionary comprehensions can actually be modeled
|
||||
in stock Python 2.2, by passing a list comprehension to the
|
||||
builtin dictionary constructor:
|
||||
|
||||
>>> dictionary([(i, chr(65+i)) for i in range(4)])
|
||||
|
||||
This has two dictinct disadvantages from the proposed syntax
|
||||
though. First, it's 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.
|
||||
TBD
|
||||
|
||||
|
||||
References
|
||||
|
|
Loading…
Reference in New Issue