Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5399

Re: I don't understand generator.send()

References <1k19ubi.dwyvio12tkc4kN%see@sig.for.address>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-05-14 18:57 -0600
Subject Re: I don't understand generator.send()
Newsgroups comp.lang.python
Message-ID <mailman.1569.1305421108.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, May 14, 2011 at 6:08 PM, Victor Eijkhout <see@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I don't understand generator.send() see@sig.for.address (Victor Eijkhout) - 2011-05-14 19:08 -0500
  Re: I don't understand generator.send() "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2011-05-15 00:38 +0000
  Re: I don't understand generator.send() Chris Angelico <rosuav@gmail.com> - 2011-05-15 10:47 +1000
    Re: I don't understand generator.send() see@sig.for.address (Victor Eijkhout) - 2011-05-14 20:40 -0500
  Re: I don't understand generator.send() Chris Rebert <crebert@ucsd.edu> - 2011-05-14 17:47 -0700
  Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 18:57 -0600
  Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 19:05 -0600
  Re: I don't understand generator.send() Chris Angelico <rosuav@gmail.com> - 2011-05-15 11:17 +1000
  Re: I don't understand generator.send() Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-14 21:03 -0600

csiph-web