python inspect.stack() 的簡單使用


1.

復制代碼
#python
# -*- encoding: utf-8 -*-
#獲取函數的名字
import inspect
def debug():
    callnamer = inspect.stack()
    print('[debug] enter: {}'.format(callnamer))

debug()
復制代碼

[debug] enter: [FrameInfo(frame=<frame object at 0x000000000096D448>, filename='E:/pythontest/sort.py', lineno=6, function='debug', code_context=[' callnamer = inspect.stack()\n'], index=0), FrameInfo(frame=<frame object at 0x00000000008F8828>, filename='E:/pythontest/sort.py', lineno=9, function='<module>', code_context=['debug()\n'], index=0)]

可以看出是一個列表

2.選取列表的第二項

復制代碼
#python
# -*- encoding: utf-8 -*-
#獲取函數的名字
import inspect
def debug():
    callnamer = inspect.stack()[1]
    print('[debug] enter: {}'.format(callnamer))

debug()
復制代碼

[debug] enter: FrameInfo(frame=<frame object at 0x00000000004A8828>, filename='E:/pythontest/sort.py', lineno=9, function='<module>', code_context=['debug()\n'], index=0)

3.選取函數的名字

復制代碼
#python
# -*- encoding: utf-8 -*-
#獲取函數的名字
import inspect
def debug():
    callnamer = inspect.stack()[1][4]
    print('[debug] enter: {}'.format(callnamer))

debug()
復制代碼

[debug] enter: ['debug()\n']


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM