1.
#python#-*- encoding: utf-8 -*-#获取函数的名字
importinspectdefdebug():
callnamer=inspect.stack()print(‘[debug] enter: {}‘.format(callnamer))
debug()
[debug] enter: [FrameInfo(frame=, filename=‘E:/pythontest/sort.py‘, lineno=6, function=‘debug‘, code_context=[‘ callnamer = inspect.stack()\n‘], index=0), FrameInfo(frame=, filename=‘E:/pythontest/sort.py‘, lineno=9, function=‘‘, code_context=[‘debug()\n‘], index=0)]
可以看出是一个列表
2.选取列表的第二项
#python#-*- encoding: utf-8 -*-#获取函数的名字
importinspectdefdebug():
callnamer= inspect.stack()[1]print(‘[debug] enter: {}‘.format(callnamer))
debug()
[debug] enter: FrameInfo(frame=, filename=‘E:/pythontest/sort.py‘, lineno=9, function=‘‘, code_context=[‘debug()\n‘], index=0)
3.选取函数的名字
#python#-*- encoding: utf-8 -*-#获取函数的名字
importinspectdefdebug():
callnamer= inspect.stack()[1][4]print(‘[debug] enter: {}‘.format(callnamer))
debug()
[debug] enter: [‘debug()\n‘]