[Python-Dev] Coroutines, generators, function calling
Phillip J. Eby
pje at telecommunity.com
Tue Oct 18 17:26:04 CEST 2005
At 12:01 PM 10/18/2005 +0100, Gustavo J. A. M. Carneiro wrote:
>def show_message(msg):
> win = create_window(msg)
> animate(win, xrange(10)) # slide down
> yield Timeout(3)
> animate(win, xrange(10, 0, -1)) # slide up
> win.destroy()
>
> This obviously doesn't work, because calling animate() produces
>another generator, instead of calling the function. In coroutines
>context, it's like it produces another coroutine, while all I wanted was
>to call a function.
Just 'yield animate(win, xrange(10))' and have the trampoline recognize
generators. See the PEP 342 trampoline example, which does this. When the
animate() is exhausted, it'll resume the "calling" function.
> I don't suppose there could be a way to make the yield inside the
>subfunction have the same effect as if it was inside the function that
>called it? Perhaps some special notation, either at function calling or
>at function definition?
Yes, it's 'yield' at the function calling. :)
More information about the Python-Dev
mailing list