I don't understand generator.send()

Victor Eijkhout see at sig.for.address
Sat May 14 21:40:22 EDT 2011


Chris Angelico <rosuav at gmail.com> wrote:

> For what you're doing, there's a little complexity. If I understand,
> you want send() to be like an ungetc call... you could do that like
> this:
> 
> 
> def ints():
>    i=0
>    while True:
>        sent=(yield i)
>        if sent is not None:
>           yield None  # This becomes the return value from gen.send()
>           yield sent  # This is the next value yielded
>        i += 1

I think this will serve my purposes.

Thanks everyone for broadening my understanding of generators.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu



More information about the Python-list mailing list