Python3 获取调用栈信息 sys._getframe

sys._getframe([depth])
Return a frame object from the call stack. If optional integer depth is given, return the frame object that many calls below the top of the stack. If that is deeper than the call stack, ValueError is raised. The default for depth is zero, returning the frame at the top of the call stack.

#executer.py

executer.exe()

#caller.py

def do_exe():
	print sys._getframe().f_code.co_filename  #当前文件名,可以通过__file__获得. getframe()方法的参数默认为0
	
	print sys._getframe(0).f_lineno #当前行号
	print sys._getframe(0).f_code.co_name  #当前函数名
	
	print sys._getframe(1).f_code.co_name #调用该函数的函数的名字
	print sys._getframe(2).f_code.co_name #调用上一个函数的调用者
	
def exe():
	do_exe()
发布了27 篇原创文章 · 获赞 3 · 访问量 1125

猜你喜欢

转载自blog.csdn.net/qq_39609993/article/details/104764095