The caller's frame is one frame higher than the current frame. You can use inspect.currentframe().f_back
to find the caller's frame.
Then use inspect.getframeinfo to get the caller's filename and line number.
import inspect
def hello():
previous_frame = inspect.currentframe().f_back
(filename, line_number,
function_name, lines, index) = inspect.getframeinfo(previous_frame)
return (filename, line_number, function_name, lines, index)
print(hello())
# ('/home/unutbu/pybin/test.py', 10, '<module>', ['hello()
'], 0)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…