extending yield's functionality

Eyal Lotem eyal at hyperroll.com
Fri Nov 2 10:08:02 EST 2001


I was excited to find out about the new yield functionality, of generator 
functions, that will be formally in 2.3

I wanted to ask if its possible to have yield receive some more information 
from the caller, between the yield stages.

An example syntax to implement this could look like:

>>> def a():
...     print "Hello"
...     b=yield 1
...     yield b*2
...     yield 3

>>> gen = a()
>>> gen.next()
1
>>> gen.next(3)
6
>>> gen.next()
3

This could make yield far more useful, and allow its use for more than just 
simple generator functions. It could then be used to implement functions 
that maintain a current-state through their internal flow, while using 
event-driven programming.

Example: A negotiation protocol can be implemented using a loop of 
recv/send operations, and yield the next stage's data each time, and accept 
new external information from the event handler (perhaps the received data).

Is this worth a PEP?




More information about the Python-list mailing list