Raymond Hettinger's latest update, slightly reformatted.

This commit is contained in:
Barry Warsaw 2002-02-07 12:08:12 +00:00
parent 1423939929
commit 27577312e1
1 changed files with 64 additions and 11 deletions

View File

@ -60,6 +60,11 @@ Rationale
keywords. These generator tools go into Python 2.3 when
generators become final and are not imported from __future__.
SourceForge contains a working, pure Python simulation of every
feature proposed in this PEP [8]. SourceForge also has a separate
file with a simulation test suite and working source code for the
examples listed used in this PEP [9].
Specification for new built-ins:
@ -118,7 +123,7 @@ Specification for new built-ins:
raise StopIteration
yield fun(*args)
def xzip( *collections ):
def xzip( *collections ): ### Code from Python Cookbook [6]
'''
xzip(...)
xzip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
@ -146,6 +151,12 @@ Specification for new built-ins:
presented and evaluated in the world prior to Python 2.2 which did
not include generators.
Note B: An alternate, simplified definition of indexed was proposed:
def indexed( collection, cnt=0, limit=sys.maxint ):
'Generates an indexed series: (0,seqn[0]), (1,seqn[1]) ...'
return xzip( xrange(cnt,limit), collection )
Specification for Generator Comprehensions:
@ -177,6 +188,37 @@ Specification for Generator Comprehensions:
indicates that brackets do not make a false suggestion and are
in fact helpful.
Note B: An iterable instance is returned by the above code. The
purpose is to allow the object to be re-started and looped-over
multiple times. This accurately mimics the behavior of list
comprehensions. As a result, the following code (provided by Oren
Tirosh) works equally well with or without 'yield':
letters = [yield chr(i) for i in xrange(ord('a'),ord('z')+1)]
digits = [yield str(i) for i in xrange(10)]
letdig = [yield l+d for l in letters for d in digits]
Note C: List comprehensions expose their looping variable and
leave the variable in the enclosing scope. The code, [str(i) for
i in range(8)] leaves 'i' set to 7 in the scope where the
comprehension appears. This behavior is by design and reflects an
intent to duplicate the result of coding a for-loop instead of a
list comprehension. Further, the variable 'i' is in a defined and
potentially useful state on the line immediately following the
list comprehension.
In contrast, generator comprehensions do not expose the looping
variable to the enclosing scope. The code, [yield str(i) for i in
range(8)] leaves 'i' untouched in the scope where the
comprehension appears. This is also by design and reflects an
intent to duplicate the result of coding a generator directly
instead of a generator comprehension. Further, the variable 'i'
is not in a defined state on the line immediately following the
list comprehension. It does not come into existence until
iteration starts. Since several generators may be running at
once, there are potentially multiple, unequal instances of 'i' at
any one time.
Specification for Generator Parameter Passing:
@ -259,14 +301,14 @@ Specification for Generator Parameter Passing:
Example of a Complex Consumer
Loop over the picture files in a directory, shrink them
one-at-a-time to thumbnail size using PIL, and send them to a lazy
consumer. That consumer is responsible for creating a large blank
image, accepting thumbnails one-at-a-time and placing them in a
5x3 grid format onto the blank image. Whenever the grid is full,
it writes-out the large image as an index print. A FlushStream
exception indicates that no more thumbnails are available and that
the partial index print should be written out if there are one or
more thumbnails on it.
one-at-a-time to thumbnail size using PIL [7], and send them to a
lazy consumer. That consumer is responsible for creating a large
blank image, accepting thumbnails one-at-a-time and placing them
in a 5x3 grid format onto the blank image. Whenever the grid is
full, it writes-out the large image as an index print. A
FlushStream exception indicates that no more thumbnails are
available and that the partial index print should be written out
if there are one or more thumbnails on it.
Example of a Producer and Consumer Used Together in a Pipelike Fashion
@ -348,6 +390,19 @@ References
[5] Dr. David Mertz's draft column for Charming Python.
http://gnosis.cx/publish/programming/charming_python_b5.txt
[6] The code fragment for xmap() was found at:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66448
[7] PIL, the Python Imaging Library can be found at:
http://www.pythonware.com/products/pil/
[8] A pure Python simulation of every feature in this PEP is at:
http://sourceforge.net/tracker/download.php?group_id=5470&atid=305470&file_id=17348&aid=513752
[9] The full, working source code for each of the examples in this PEP
along with other examples and tests is at:
http://sourceforge.net/tracker/download.php?group_id=5470&atid=305470&file_id=17412&aid=513756
Copyright
@ -360,5 +415,3 @@ mode: indented-text
indent-tabs-mode: nil
fill-column: 70
End: