Entry
Function which knows who has called it
Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 294, Michael P. Reilly
"""
Packages: miscellaneous.introspection
"""
"""
: Is if possible in Python to have a function that prints the name
: of the function that called it. (I'm thinking of using this in
: some debugging code I want to write).
The information is captured in the frames in the traceback.
"""
def you_rang():
import sys
try:
raise RuntimeError
except RuntimeError:
exc, val, tb = sys.exc_info()
frame = tb.tb_frame.f_back
del exc, val, tb
try:
return frame.f_back.f_code.co_name
except AttributeError: # called from the top
return None
def f():
WhoCalledMe = you_rang()
print WhoCalledMe
def p(): # p calls f
f()
p()