python 获取函数调用者


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]即可。

 

 




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM