在Python中,不使用traceback
模块,有没有办法从该函数内确定该函数的名称?
假设我有一个模块foo
有一个函数bar
。执行时foo.bar()
, 有没有办法bar
知道bar
的名字?或者更好的是,foo.bar
's name?
#foo.py
def bar():
print "my name is", __myname__ # <== how do I calculate this at runtime?
import inspect
def foo():
print(inspect.stack()[0][3])
print(inspect.stack()[1][3]) # will give the caller of foos name, if something called foo
foo()
output:
foo
<module_caller_of_foo>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)