考虑以下伪代码:
func1():
func2() #func2 is called inside func1
Run Code Online (Sandbox Code Playgroud)
我的问题是,在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()
Run Code Online (Sandbox Code Playgroud)
输出:
called from <module>
called from func1
Run Code Online (Sandbox Code Playgroud)