I'm trying to learn more about generator functions in Python. To my knowledge, generator functions don't fully return until there are no more yield
calls, so a stack frame exists in the generator returned by the function.
Stack frames should have references to a callable function, so my question is: how can I get that callable function from the generator?
When running the code below, I have a generator function, test()
.
def test():
for i in range(10):
yield i
generator = test()
In this example, is there any way to get the callable function test()
from generator
?
After looking at this answer, it seems like CPython keeps track of some of these like generator.frame
and generator.code
, however, I can't seem to convert those objects into functions.
I need the callable function. Something like this:
func = generator.something_special
new_generator = func()