How do I print functions as they are called?

You can do this with a trace function (props to Spacedman for improving the original version of this to trace returns and use some nice indenting):

def tracefunc(frame, event, arg, indent=[0]):
      if event == "call":
          indent[0] += 2
          print("-" * indent[0] + "> call function", frame.f_code.co_name)
      elif event == "return":
          print("<" + "-" * indent[0], "exit function", frame.f_code.co_name)
          indent[0] -= 2
      return tracefunc

import sys
sys.setprofile(tracefunc)

main()   # or whatever kicks off your script

Note that a function’s code object usually has the same name as the associated function, but not always, since functions can be created dynamically. Unfortunately, Python doesn’t track the function objects on the stack (I’ve sometimes fantasized about submitting a patch for this). Still, this is certainly “good enough” in most cases.

If this becomes an issue, you could extract the “real” function name from the source code—Python does track the filename and line number—or ask the garbage collector find out which function object refers to the code object. There could be more than one function sharing the code object, but any of their names might be good enough.

Coming back to revisit this four years later, it behooves me to mention that in Python 2.6 and later, you can get better performance by using sys.setprofile() rather than sys.settrace(). The same trace function can be used; it’s just that the profile function is called only when a function is entered or exited, so what’s inside the function executes at full speed.

Leave a Comment