[Python-Dev] PEP 380 (yield from a subgenerator) comments

Antoine Pitrou solipsis at pitrou.net
Thu Mar 26 11:56:29 CET 2009


Guido van Rossum <guido <at> python.org> writes:
> 
> That's stating it a little too strongly. Phillip has shown how with
> judicious use of decorators and helper classes you can get a
> reasonable approximation, and I think Twisted uses something like
> this, so it's not just theory. I think the best you can do without new
> syntax though is still pretty cumbersome and brittle, which is why I
> have encouraged your PEP.

It remains to be seen whether Twisted and other libraries (Kamaelia?) can
benefit from this PEP. There seems to be a misunderstanding as to how generators
are used in Twisted. There isn't a global "trampoline" to schedule generators
around. Instead, generators are wrapped with a decorator (*) which collects each
yielded value (it's a Deferred object) and attaches to it a callback which
resumes (using send()) the execution of the generator whenever the Deferred
finally gets its value. The wrapped generator, in turn, looks like a normal
Deferred-returning function to outside code.

Therefore, there is no nesting problem and "yield from" doesn't seem to be
useful here. This has been confirmed to me by a Twisted developer on IRC (he
pointed out, however, a streaming XML parser where "yield from" could save a
couple of lines of code).

(*) inlineCallbacks:
http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.html#inlineCallbacks
http://enthusiasm.cozy.org/archives/2009/03/python-twisteds-inlinecallbacks

Regards

Antoine.




More information about the Python-Dev mailing list