Is there any way to programmatically find the frame that a function was defined in? I'm looking for something like a function get_defining_frame
that would work like this:
def foo():
def bar():
pass
return bar
bar = foo()
get_defining_frame(foo) # should be the same as inspect.currentframe()
get_defining_frame(bar) # should be the frame created by the call to foo
Thanks!
Frames are created/deleted on every function call. So when you call:
get_defining_frame(bar)
in your example code, the frame where bar
was created is already gone.
Function objects don't hold a reference to the frame they were defined in. It's a good thing because it would keep all other local variables from being freed.
Why do you need this?