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

dmitrey dmitrey15 at gmail.com
Fri Dec 30 13:43:50 EST 2011


On Dec 30, 8:35 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> 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

Thank you. And what should I do to get function by itself instead of
its string name, e.g. I want to know does this function is my_func or
any other? For example, I would like to check is this function Python
sum(), or maybe numpy.sum(), or anything else?
Regards, D.



More information about the Python-list mailing list