I’ve read that in CPython, the interpreter stack (the list of Python functions called to reach this point) is mixed with the C stack (the list of C functions that were called in the interpreter’s own code). If so, then how are generators and coroutines implemented? How do they remember their execution state? Does CPython copy each generator’s / coroutine’s stack to and from an OS stack? Or does CPython simply keep the generator’s topmost stack frame on the heap, since the generator can only yield from that topmost frame?
I’ve read that in CPython, the interpreter stack (the list of Python functions called
Share
The
yieldinstruction 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.