Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are generators and coroutines implemented in CPython?

People also ask

How are generators implemented?

Normally, generator functions are implemented with a loop having a suitable terminating condition. Let's take an example of a generator that reverses a string. In this example, we have used the range() function to get the index in reverse order using the for loop.

What are generators and coroutines in Python?

In Python, coroutines are similar to generators but with few extra methods and slight changes in how we use yield statements. Generators produce data for iteration while coroutines can also consume data. whatever value we send to coroutine is captured and returned by (yield) expression.

Are coroutines generators?

Coroutines are Generators, but their yield accepts values. Coroutines can pause and resume execution (great for concurrency).

What differentiates a generator from a coroutine?

A generator is essentially a cut down (asymmetric) coroutine. The difference between a coroutine and generator is that a coroutine can accept arguments after it's been initially called, whereas a generator can't.


The notion that Python's stack and C stack in a running Python program are intermixed can be misleading.

The Python stack is something completely separated than the actual C stack used by the interpreter. The data structures on Python stack are actually full Python "frame" objects (that can even be introspected and have some attributes changed at run time). This stack is managed by the Python virtual machine, which itself runs in C and thus have a normal C program, machine level, stack.

When using generators and iterators, the interpreter simply stores the respective frame object somewhere else than on the Python program stack, and pushes it back there when execution of the generator resumes. This "somewhere else" is the generator object itself.Calling the method "next" or "send" on the generator object causes this to happen.


The yield instruction takes the current executing context as a closure, and transforms it into an own living object. This object has a __iter__ method which will continue after this yield statement.

So the call stack gets transformed into a heap object.