Get the name of an outer function from inner function in python
本问题已经有最佳答案,请猛点这里访问。
考虑以下伪代码:
1 2 | func1(): func2() #func2 is called inside func1 |
我的问题是,在func2中,我可以访问它调用的函数的名称吗?在这种情况下,func1?谢谢!
1 2 3 4 5 6 7 8 9 10 11 12 | import inspect def func2(): cframe = inspect.currentframe() func = inspect.getframeinfo(cframe.f_back).function print 'called from ' + func def func1(): func2() func2() func1() |
输出:
1 2 | called from <module> called from func1 |