How can I get a source code of Python function which was declared by using 'exec' inside other function?
I need to get a source code of Python function which was declared via using exec inspect.getsource can't help me with that. Simplified Example: from inspect import getsource def declare_func(): func_code=\ '''def new_func(): print("Hello, world!")''' exec(func_code) return locals()['new_func'] func = declare_func() print('=run=') func() print('=func=') print(func) print('=code=') print(getsource(func)) Output: =run= Hello, world! =func= <function new_func at 0x000001DF77173E20> =code= ... OSError: could not get source code How can I get source code of the 'func' ? Note1: I can't avoid using ex
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.