Opposite of yield?
glc at well.com
Fri Sep 12 17:00:25 CEST 2003
On Wed, 10 Sep 2003 12:40:37 -0700, achrist at easystreet.com wrote:
>The yield statement looks to be a big step toward some kind of
>lightweight concurrency-oriented programming in python. Is there
>any similarly nice way to do the opposite of yield, just sit around
>(perhaps in the middle of a loop) and wait until some other routine
>(unknown to the waiting module) hurls a value in?
>If this is not implemented now, is there any chance that it's on the
>list of things to come?
You can use a generator as the body of a function. That is, the generator is
only ever accessed through the function, and the function and the generator have
some agreed on way to pass parameters. I.e.:
'''getline accumulates text until it has a complete line (signaled
by either a newline (\\n) or a null (\\0)). It then passes the
line (without trailing \n) to output'''
genargs = [None]
genargs = text
line = ''
text, = genargs
lines = text.splitlines(True)
lines = line+lines
for line in lines[:-1]:
line = lines[-1]
if line[-1] in '\n\0':
if line[-1] == '\n':
line = line[:-1]
line = ''
gennext = gen().next
lineprinter = getline(printer)
lineprinter('hello') #nothing output
lineprinter(' world\n') #prints 'hello world'
More information about the Python-list