Get functions from python generator

Michael M.

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()
0x263A

You can use the __name__ attribute to get the name of the original function then access it inside locals provided it's still in scope.

def test():
    for i in range(10):
        yield i


generator = test()
print(list(generator)) 
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


func_name = generator.__name__ 
new_generator = locals()[func_name]()
print(func_name, list(new_generator)) 
# test [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Understanding generator functions in Python

Python get the last element from generator items

How to get Dart generator functions to act like Python?

Two simultaneous generator functions, Python

How to make simple python generator from nested generator, "Generator in Generator"?

How to get one value at a time from a generator function in Python?

Get the generator function's name from a generator

Get an iterable generator from an async generator

Python generator and set(generator) get different results

Generator Functions being called from a normal function

How to prevent babel from transpiling generator functions

Type-able Functions From Generator Function

Issues with python data generator functions in PyTest

Issue in accessing the output using generator functions in python

What can you use Python generator functions for?

dynamic call of functions and generator function (python)

Get the nth item of a generator in Python

Generator and functions

is for/while loop from python is a generator

Python format string from generator

Python `yield from`, or return a generator?

Yield values from generator from async functions without awaiting

What is the shortest Python code to get a maximum from the two discrete functions?

How to get a list of classes and functions from a python file without importing it

How to get functions from a python file without import

How to execute functions in Python at the same time and get a values from them?

How to get singular value from a generator?

Get single yield value from iterator/generator

How to get Cartesian product in Python using a generator?