python 获取函数调用者

时间:2019-03-21 18:23:38   收藏:0   阅读:367
import traceback

def _mode():
	print "hi---------------------------"
	print traceback.extract_stack()[-2][2]

def fun1():
	_mode()

def fun2():
	_mode()


if __name__ == ‘__main__‘:
	fun2()
	fun1()
 
 
traceback.extract_stack() output>
 
[(‘test_traceback.py‘, 16, ‘<module>‘, ‘fun1()‘), (‘test_traceback.py‘, 8, ‘fun1‘, ‘_mode()‘), (‘test_traceback.py‘, 5, ‘_mode‘, ‘print traceback.extract_stack()‘)]
 
 
  由三个数组组成list,我们只需要取[-2][2]即可。

 

 

来自为知笔记(Wiz)



原文:https://www.cnblogs.com/wangbaojun/p/10573492.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!