考虑以下伪代码:
func1():
func2() #func2 is called inside func1
我的问题是,在func2函数中,我能否访问调用它的函数的名称?在这种情况下,是func1吗?谢谢!
func1():
func2() #func2 is called inside func1
我的问题是,在func2函数中,我能否访问调用它的函数的名称?在这种情况下,是func1吗?谢谢!
import inspect
def func2():
cframe = inspect.currentframe()
func = inspect.getframeinfo(cframe.f_back).function
print 'called from ' + func
def func1():
func2()
func2()
func1()
输出:
called from <module>
called from func1