I don't understand generator.send()

Ian Kelly ian.g.kelly at gmail.com
Sat May 14 20:57:56 EDT 2011


On Sat, May 14, 2011 at 6:08 PM, Victor Eijkhout <see at sig.for.address> wrote:
> I thought the send call would push the value "2" at the front of the
> queue. Instead it coughs up the 2, which seems senseless to me.
>
> 1/ How should I view the send call? I'm reading the manual and dont' get
> it

There is no queue unless you create one inside the generator.  The
generator by itself behaves more like a coroutine.

> 2/ Is there a way to push something in the generator object? So that it
> becomes the next yield expression? In my code I was hoping to get
> 0,1,2,3,4,5,2,6,7 as yield expressions.

This will do what you're asking for:

def ints():
    i=0
    while True:
        next_yield = (yield i)
        while next_yield is not None:
            next_yield = (yield next_yield)
        i += 1

However, I don't think this is what you want.  The send call returns a
yield expression, which will then be the value that you just passed
in, which seems a bit silly.  Probably you want something more like
this:

def ints():
    i=0
    while True:
        next_yield = (yield i)
        while next_yield is not None:
            yield None
            next_yield = (yield next_yield)
        i += 1

Then the send() call will return None, and the next next() call will
return the value you passed in.  Note though that this is too simple
to work correctly if you call send() more than once before calling
next() again.

In general, I think it is a bad idea to mix calling next() and send()
on the same generator.  It makes the generator logic too complicated,
and I think it's better just to create a stateful iterator class
instead, where send() and next() are two entirely separate methods.

Cheers,
Ian



More information about the Python-list mailing list