How to get function string name from i-th stack position?

Tim Chase python.list at tim.thechases.com
Fri Dec 30 13:35:53 EST 2011


On 12/30/11 11:51, dmitrey wrote:
> how to get string name of a function that is n levels above
> the current Python interpreter position?

Use the results of traceback.extract_stack()

   from traceback import extract_stack
   def one(x):
     print "one", x
     stk = extract_stack()
     for mod, lineno, fun_name, call_code_text in stk:
       print "[%s:%i] in %s" % (mod, lineno, fun_name)
   def two(x):
     print "two", x
     one(x)
   def three(x):
     print "three", x
     two(x)
   three("Hi")


-tkc






More information about the Python-list mailing list