Generators versus Coroutines

Lenard Lindstrom len-1 at telus.net
Mon Aug 16 17:11:01 EDT 2004


firemoth at gmail.com (Timothy Fitz) writes:

> Lenard Lindstrom <len-1 at telus.net> wrote in message news:<zn4w2y3g.fsf at telus.net>...
> > Nick Patavalis <npat at efault.net> writes:
> > 
> > > On 2004-08-14, Paul Rubin <> wrote:
> > > >
> > > > You can't yield across multiple levels of function calls.  That's why
> > > > they're called "simple generators" instead of just "generators".
> > > >
> > > 
> > > For me it would be *tremendously* usefull if one could yield across
> > > multiple levels of function calls. If this was supported, then most
> > > uses of O/S treads could be replaced by generators.
> > > 
> > > Does anyone know if such a feature is planned?
> > > 
> > Having followed the development of Prothon it is my understanding that
> > such a thing is not possible in CPython for two reasons.
[Snip]
> 
> I disagree, it seems to me I could make a pure python based
> implementation with some funky state handling classes and exceptions.
> Granted, it wouldn't look nice (all your 'locals' would be in an
> object, and you'd have to call like tyield())

I should have been clearer. It is not possible with the current generator
function declaration syntax. A new keyword or something would have to be
added to make it work. If a keyword, then the new syntax would not be
backwards compatible with the current. As for doing it in Python,
coroutines can be implemented using threads. So yes it is possible.
But it is not necessary efficient.

Lenard Lindstrom
<len-l at telus.net>



More information about the Python-list mailing list