A very simplified version of what happens (for the CPython interpreter):
Every time a method is called in Python a "frame" is added to the stack, after a method returns something the interpreter pops the last frame from the stack and continues execution of the previous frame with the return value injected in place of the method call
To get the previous frame you can call sys._getframe(1)
(0 would get the current frame, 1 gets the previous frame). The inspect module provides a method getframeinfo
that returns some useful information about the frame including the filename. This can be combined like so
import inspect
import sys
def foo():
print('Called from', inspect.getframeinfo(sys._getframe(1)).filename)
Whenever foo
is called it will print the filename of the calling method