[Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

patrice laporte zepangolin at gmail.com
Sat Feb 13 20:38:45 CET 2010


2010/2/13 Kent Johnson <kent37 at tds.net>

>
>
> >Here is a way to do it using the traceback module. Under the hood it
> >is still using implementation details contained in traceback and stack
> >frame objects but the public interface in the traceback module should
> >be stable.
>
> >import sys, traceback
>
> >def whoami():
> >   stack = traceback.extract_stack(limit=2)
> >   print "Who call me ? : ", stack[0][2]
> >   print "Who am I ? : ", stack[1][2]
>
> >def print_caller_name():
> >   whoami()
>
> >if __name__ == "__main__":
> >  print_caller_name()
>
> >Kent
>
Thanks for the help,  I will look closer at the traceback module and at your
solution

Patrice
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100213/c7ebb5b7/attachment.htm>


More information about the Tutor mailing list