The traceback module allows you to traceback stack so you can see how you reached the current frame frame. If you want, you can expand it to print the caller as far from the stack as possible:
import traceback def _trace(): stack = traceback.extract_stack()[-3:-1] path, line, in_func, _instr = stack[0] print 'called from %s in func %s at line %s' % (path, in_func, line) def bar(): _trace() def foo(): bar() baz() def baz(): bar() bar() foo()
Output:
called from hello.py in func <module> at line 20 called from hello.py in func foo at line 14 called from hello.py in func baz at line 18
samplebias
source share