function which knows who has called it.

Michael P. Reilly arcege at
Tue Aug 17 02:53:10 CEST 1999

Phil Hunt <philh at> wrote:

: 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
      raise RuntimeError
    except RuntimeError:
      exc, val, tb = sys.exc_info()
      frame = tb.tb_frame.f_back
      del exc, val, tb
      return frame.f_back.f_code.co_name
    except AttributeError:  # called from the top
      return None

  def f():
    WhoCalledMe = you_rang()


More information about the Python-list mailing list