[Python-Dev] Breaking off Enhanced Iterators PEP from PEP 340
Michele Simionato
michele.simionato at gmail.com
Sat May 7 07:45:30 CEST 2005
On 5/6/05, Steven Bethard <steven.bethard at gmail.com> wrote:
> FWIW, I'm +1 on this. Enhanced Iterators
> * updates the iterator protocol to use .__next__() instead of .next()
> * introduces a new builtin next()
> * allows continue-statements to pass values to iterators
> * allows generators to receive values with a yield-expression
> The first two are, I believe, how the iterator protocol probably
> should have been in the first place. The second two provide a simple
> way of passing values to generators, something I got the impression
> that the co-routiney people would like a lot.
Thank you for splitting the PEP. Conceptually, the "coroutine" part
has nothing to do with blocks and it stands on its own, it is right
to discuss it separately from the block syntax.
Personally, I do not see an urgent need for the block syntax (most of
the use case can be managed with decorators) nor for the "couroutine"
syntax (you can already use Armin Rigo's greenlets for that).
Anyway, the idea of passing arguments to generators is pretty cool,
here is some code I have, adapted from Armin's presentation at the
ACCU conference:
from py.magic import greenlet
def yield_(*args):
return greenlet.getcurrent().parent.switch(*args)
def send(key):
return process_commands.switch(key)
@greenlet
def process_commands():
while True:
line = ''
while not line.endswith('\n'):
line += yield_()
print line,
if line == 'quit\n':
print "are you sure?"
if yield_() == 'y':
break
process_commands.switch() # start the greenlet
send("h")
send("e")
send("l")
send("l")
send("o")
send("\n")
send("q")
send("u")
send("i")
send("t")
send("\n")
Michele Simionato
More information about the Python-Dev
mailing list