function which knows who has called it.

Michael P. Reilly arcege at shore.net
Mon Aug 16 20:53:10 EDT 1999


Phil Hunt <philh at vision25.demon.co.uk> 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
    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()

  -Arcege






More information about the Python-list mailing list