Generators versus Coroutines

Lenard Lindstrom len-1 at telus.net
Mon Aug 16 16:39:13 EDT 2004


Nick Patavalis <npat at efault.net> writes:

> On 2004-08-15, Lenard Lindstrom <len-1 at telus.net> wrote:
> >
> > Having followed the development of Prothon it is my understanding that
> > such a thing is not possible in CPython for two reasons. The first is
> > that the yield statement itself defines a function declaration as a
> > generator. Without a specific "gen" declarator keyword as in Prothon.
> >
> 
> Oh, I see. Then I have to say, this is an unbelievable screw-up in the
> language specification. Being able to yield from within multiple
> function-call levels is essential for any non-trivial use of
> generators. Without this generators can only be used for simple stuff
> such as to creates lists dynamically.
>
That is the idea. Generators are a shorthand way to implement iterators.
But relative to other methods they are fast.

> > The second reason is a python function call in CPython involves an
> > actual recursive C call of the interpreter.
> 
> What!?! CPython uses the *C-stack* !!?!?!

Yes.

> Is there a reason for this?

I do not know for sure. I believe performance is one reason. Another is
portablility. Having a python program call an external function written
in C which in turn executes some Python code requires a recursive interpreter
call. Handling the C stack in this case would require non-standard C
libraries. Maybe someone actually involved in maintaining CPython will
give a better answer.

> I mean, for a weekend toy-project interpreter such an aproach would be
> understandable, but for the primary Python interpreter! There must be
> a reason I don't understand...
> 
You are not alone. Christian Tismer has a modified version of CPython
known as Stackless. It supports light weight threads that can be used
as coroutines. For compatibility reasons, though, Stackless generators are
justs like CPython's. But Stackless is to be superseded by PyPy, which
is under development now.

Lenard Lindstrom
<len-l at telus.net>



More information about the Python-list mailing list