Some rearrangements and fixes based on recent comments.

This commit is contained in:
Barry Warsaw 2001-10-31 15:09:55 +00:00
parent 4574958961
commit e678d918f3
1 changed files with 28 additions and 31 deletions

View File

@ -35,10 +35,9 @@ Rationale
There are times when you have some data arranged as a sequences of 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. length-2 sequences, and you want to turn that into a dictionary.
In Python 2.2, the dictionary() constructor will take an optional In Python 2.2, the dictionary() constructor accepts an argument
keyword argument that indicates specifically to interpret a that is a sequence of length-2 sequences, used as (key, value)
sequences of length-2 sequences as key/value pairs, and turn them pairs to initialize a new dictionary object.
into a dictionary.
However, the act of turning some data into a sequence of length-2 However, the act of turning some data into a sequence of length-2
sequences can be inconvenient or inefficient from a memory or sequences can be inconvenient or inefficient from a memory or
@ -54,6 +53,24 @@ Rationale
for loop. 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 Examples
>>> print {i : chr(65+i) for i in range(4)} >>> print {i : chr(65+i) for i in range(4)}
@ -72,6 +89,12 @@ Examples
>>> print invert(d) >>> print invert(d)
{'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3} {'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 Open Issues
@ -99,36 +122,10 @@ Open Issues
from? The shortcut probably doesn't save much typing, and comes from? The shortcut probably doesn't save much typing, and comes
at the expense of legibility, so it's of dubious value. 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 Implementation
The semantics of dictionary comprehensions can actually be modeled TBD
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.
References References