首页 > 编程语言 > 详细

python inspect.stack() 的简单使用

时间:2019-12-09 18:07:09      阅读:214      评论:0      收藏:0      [点我收藏+]

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‘]

python inspect.stack() 的简单使用

原文:https://www.cnblogs.com/taosiyu/p/12012359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!