[Python-Dev] PEP 380 - return value question and prototype implementation (was Thoughts fresh after EuroPython)

P.J. Eby pje at telecommunity.com
Sun Jul 25 05:46:16 CEST 2010


At 08:21 PM 7/24/2010 -0700, Guido van Rossum wrote:
>FWIW, the thing that was harder to debug when I tried to write some
>code involving generators and a trampoline recently, was thinking of a
>function as a generator without actually putting a yield in it
>(because a particular version of a coroutine pattern didn't need to
>block at all). Monocle uses a decorator to flag all coroutines which
>fixes this up in the right way, which I think is clever, but I'm torn
>about the need to flag every coroutine with a decorator -- Monocle
>makes the decorator really short (@_o) because, as Raymond (not
>Monocle's author but its advocate at EuroPython) said, "you'll be
>using this hundreds of times". Which I find disturbing in itself.

I haven't used Monocle, but in all the libraries I've written myself 
for this sort of thing (Trellis and peak.events), a decorator is only 
required for a generator that is a "root" task; everything else is 
just a normal generator.

For example, in Trellis you use @Task.factory to mark a function as 
spawning an independent task each time it's called, but subgenerator 
functions called within the task don't need to be marked, and in fact 
the "yield from" is just a "yield" - the trampoline expects all 
yields of generators to be subgenerator calls.  (PEP 380 can't do 
this of course, since it also doubles as a sort of 'yield *' - i.e., 
you may care about the yielded values)

Note, though, that even in the sketch I just gave, you don't *really* 
need to decorate every function, just the ones that need to be called 
from *non*-decorated functions...  i.e. "root" coroutines.  Even 
then, you could *still* skip the decorator and replace:

       an_iter = decorated_root_function()

with:

       an_iter = From(undecorated_root_function())

and not need to decorate *anything*.





More information about the Python-Dev mailing list