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学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#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']

用一个例子来演示会更加清晰

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐