[Python-checkins] CVS: python/nondist/peps pep-0274.txt,1.2,1.3
Barry Warsaw
bwarsaw@users.sourceforge.net
Wed, 31 Oct 2001 07:09:57 -0800
Update of /cvsroot/python/python/nondist/peps
In directory usw-pr-cvs1:/tmp/cvs-serv19480
Modified Files:
pep-0274.txt
Log Message:
Some rearrangements and fixes based on recent comments.
Index: pep-0274.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0274.txt,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** pep-0274.txt 2001/10/29 18:46:59 1.2
--- pep-0274.txt 2001/10/31 15:09:55 1.3
***************
*** 36,43 ****
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.
However, the act of turning some data into a sequence of length-2
--- 36,42 ----
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 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
***************
*** 55,58 ****
--- 54,75 ----
+ 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
***************
*** 73,77 ****
--- 90,100 ----
{'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
***************
*** 100,133 ****
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.
--- 123,130 ----
at the expense of legibility, so it's of dubious value.
Implementation
! TBD