On Sat, May 14, 2011 at 6:47 PM, Chris Angelico <ros...@gmail.com> wrote:
> def ints():
>    i=0
>    queue=[]
>    while True:
>        if queue:  # see other thread, this IS legal and pythonic and
> quite sensible
>            sent=(yield queue.pop(0))
>        else:
>            sent=(yield i)
>            i+=1
>        if sent is not None:
>            yield None  # This is the return value from gen.send()
>            queue.append(sent)
>
> With this generator, you maintain a queue of sent values (if you want
> it to be a LIFO stack rather than a FIFO queue, just change the pop(0)
> to just pop()), and if the queue's empty, it produces sequential
> integers. (Incidentally, the sent values don't have to be integers. I
> leave it to you to decide whether that's any use or not.)

Actually, this won't work, because the value of the "yield None" gets
ignored.  Thus if you try to call send() twice in a row, the generator
the treats second send() as if it were a next(), and it is not
possible to have more than one item in the queue.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to