Raymond Hettinger's latest updates.
This commit is contained in:
parent
f2115cc852
commit
a50cc106f7
74
pep-0279.txt
74
pep-0279.txt
|
@ -7,7 +7,7 @@ Status: Draft
|
|||
Type: Standards Track
|
||||
Created: 30-Jan-2002
|
||||
Python-Version: 2.3
|
||||
Post-History:
|
||||
Post-History:
|
||||
|
||||
|
||||
Abstract
|
||||
|
@ -45,7 +45,7 @@ Rationale
|
|||
2. Provide a generator alternative to list comprehensions [3]
|
||||
making generator creation as convenient as list creation.
|
||||
|
||||
3. Extend the syntax of the 'yield' keyword to enable two way
|
||||
3. Extend the syntax of the 'yield' keyword to enable generator
|
||||
parameter passing. The resulting increase in power simplifies
|
||||
the creation of consumer streams which have a complex execution
|
||||
state and/or variable state.
|
||||
|
@ -57,7 +57,8 @@ Rationale
|
|||
All of the suggestions are designed to take advantage of the
|
||||
existing implementation and require little additional effort to
|
||||
incorporate. Each is backward compatible and requires no new
|
||||
keywords.
|
||||
keywords. These generator tools go into Python 2.3 when
|
||||
generators become final and are not imported from __future__.
|
||||
|
||||
|
||||
Specification for new built-ins:
|
||||
|
@ -149,33 +150,20 @@ Specification for new built-ins:
|
|||
Specification for Generator Comprehensions:
|
||||
|
||||
If a list comprehension starts with a 'yield' keyword, then
|
||||
express the remainder of the statement as generator. For example:
|
||||
express the comprehension with a generator. For example:
|
||||
|
||||
g = [yield (len(line),line) for line in file.readline() if len(line)>5]
|
||||
print g.next()
|
||||
print g.next()
|
||||
g = [yield (len(line),line) for line in file if len(line)>5]
|
||||
|
||||
This would be implemented as if it had been written:
|
||||
|
||||
def __temp_gen():
|
||||
for line in file.readline():
|
||||
if len(line) > 5:
|
||||
yield (len(line), line)
|
||||
g = __temp_gen()
|
||||
print g.next()
|
||||
print g.next()
|
||||
class __Temp:
|
||||
def __iter__(self):
|
||||
for line in file:
|
||||
if len(line) > 5:
|
||||
yield (len(line), line)
|
||||
g = __Temp()
|
||||
|
||||
Note A: There is a difference in the above implementation as
|
||||
compared to list comprehensions. For a generator comprehension,
|
||||
the variables are created in a separate scope while list
|
||||
comprehensions use the enclosing scope. If this PEP is accepted,
|
||||
the parser should generate byte code that eliminates this
|
||||
difference by passing the line variable in the enclosing scope and
|
||||
using that same variable passed by reference inside the generator.
|
||||
This will make the behavior of generator comprehension identical
|
||||
to that of list comprehensions.
|
||||
|
||||
Note B: There is some debate about whether the enclosing brackets
|
||||
Note A: There is some debate about whether the enclosing brackets
|
||||
should be part of the syntax for generator comprehensions. On the
|
||||
plus side, it neatly parallels list comprehensions and would be
|
||||
immediately recognizable as a similar form with similar internal
|
||||
|
@ -190,7 +178,7 @@ Specification for Generator Comprehensions:
|
|||
in fact helpful.
|
||||
|
||||
|
||||
Specification for two-way Generator Parameter Passing:
|
||||
Specification for Generator Parameter Passing:
|
||||
|
||||
1. Allow 'yield' to assign a value as in:
|
||||
|
||||
|
@ -209,7 +197,7 @@ Specification for two-way Generator Parameter Passing:
|
|||
The control flow is unchanged by this proposal. The only change
|
||||
is that a value can be sent into the generator. By analogy,
|
||||
consider the quality improvement from GOSUB (which had no argument
|
||||
passing mechanism) to modern procedure calls (which pass in
|
||||
passing mechanism) to modern procedure calls (which can pass in
|
||||
arguments and return values).
|
||||
|
||||
Most of the underlying machinery is already in place, only the
|
||||
|
@ -234,13 +222,13 @@ Specification for two-way Generator Parameter Passing:
|
|||
threading module with its attendant mutexes, semaphores, and data
|
||||
queues. A class-based approach competes well when there are no
|
||||
complex execution states or variable states. When the complexity
|
||||
increases, generators with two-way communication are much simpler
|
||||
increases, generators with parameter passing are much simpler
|
||||
because they automatically save state (unlike classes which must
|
||||
explicitly save the variable and execution state in instance
|
||||
variables).
|
||||
|
||||
|
||||
Example of a Complex Consumer
|
||||
Example of a Complex Consumer
|
||||
|
||||
The encoder for arithmetic compression sends a series of
|
||||
fractional values to a complex, lazy consumer. That consumer
|
||||
|
@ -281,6 +269,15 @@ Specification for two-way Generator Parameter Passing:
|
|||
more thumbnails on it.
|
||||
|
||||
|
||||
Example of a Producer and Consumer Used Together in a Pipelike Fashion
|
||||
|
||||
'Analogy to: source | upper | sink'
|
||||
sink = sinkgen()
|
||||
sink.next()
|
||||
for word in source():
|
||||
sink.next( word.upper() )
|
||||
|
||||
|
||||
Specification for Generator Exception Passing:
|
||||
|
||||
Add a .throw(exception) method to the resulting generator as in:
|
||||
|
@ -311,22 +308,29 @@ Specification for Generator Exception Passing:
|
|||
putting the exception in another location. The word throw is
|
||||
already associated with exceptions in other languages.
|
||||
|
||||
Note B: The throw syntax should exactly match raise's syntax including:
|
||||
raise string g.throw(string)
|
||||
raise string, data g.throw(string,data)
|
||||
raise class, instance g.throw(class,instance)
|
||||
raise instance g.throw(instance)
|
||||
raise g.throw()
|
||||
|
||||
|
||||
References
|
||||
|
||||
|
||||
[1] PEP 255 Simple Generators
|
||||
http://python.sourceforge.net/peps/pep-0255.html
|
||||
|
||||
[2] PEP 212 Loop Counter Iteration
|
||||
http://python.sourceforge.net/peps/pep-0212.html
|
||||
|
||||
[3] PEP 202 List Comprehensions
|
||||
[3] PEP 202 List Comprehensions
|
||||
http://python.sourceforge.net/peps/pep-0202.html
|
||||
|
||||
[4] There have been several discussion on comp.lang.python which helped
|
||||
tease out these proposals:
|
||||
|
||||
Indexed Function
|
||||
Indexed Function
|
||||
http://groups.google.com/groups?hl=en&th=33f778d92dd5720a
|
||||
|
||||
Xmap, Xfilter, Xzip and Two-way Generator Communication
|
||||
|
@ -339,10 +343,10 @@ References
|
|||
http://groups.google.com/groups?hl=en&th=215e6e5a7bfd526&rnum=2
|
||||
|
||||
Discussion Draft of this PEP
|
||||
http://groups.google.com/groups?hl=en&th=df8b5e7709957eb7
|
||||
http://groups.google.com/groups?hl=en&th=df8b5e7709957eb7
|
||||
|
||||
[5] Dr. David Mertz's draft column for Charming Python.
|
||||
href="http://gnosis.cx/publish/programming/charming_python_b5.txt
|
||||
http://gnosis.cx/publish/programming/charming_python_b5.txt
|
||||
|
||||
|
||||
Copyright
|
||||
|
@ -356,3 +360,5 @@ mode: indented-text
|
|||
indent-tabs-mode: nil
|
||||
fill-column: 70
|
||||
End:
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue