[Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?
patrice laporte
zepangolin at gmail.com
Sat Feb 13 16:56:08 CET 2010
Hi,
Being in an exeption of my own, I want to print the name of the caller, and
I'm looking for a way to have it.
I've found a lot of recipe all of them using what seems to be, according to
me, a not so good trick : all those recipe make use of
sys._getframe(1).f_code.co_name, such as in this exemple :
import sys
def whoami():
print "Who call me ? : ", sys._getframe(1).f_code.co_name
print "Who am I ? : ", sys._getframe(0).f_code.co_name
def print_caller_name():
whoami()
if __name__ == "__main__":
print_caller_name()
With the following result, as expected :
Who call me ? : print_caller_name
Who am I ? : whoami
A closer look at the documentation about sys._getframe at
http://docs.python.org/library/sys.html make me think it's not a good idea,
because of the note :
"*CPython implementation detail:* This function should be used for internal
and specialized purposes only. *It is not guaranteed to exist in all
implementations of Python*."
So, this trick works today, but what about tomorow ?
Is anybody have an idea how to get the the same result without _getframe ?
Thanks
Patrice.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100213/8f11e7dd/attachment-0001.htm>
More information about the Tutor
mailing list