importinspectdefget_function_name():# 获取当前函数的调用栈stack=inspect.stack()# 获取调用栈中的第二个元素,即调用者的信息caller=stack[1]# 获取调用者的函数名称function_name=caller.functionreturnfunction_namedefexample_function():function_name=get_function_name()print("调用我函数的函数名称是:",func...
Caller function name: main_function 1. 序列图 下面是一个使用mermaid语法表示的序列图,展示了获取调用函数的函数名的过程: Target FunctionCaller FunctionTarget FunctionCaller Function调用函数返回结果 类图 下面是一个使用mermaid语法表示的类图,展示了inspect模块中的currentframe和getframeinfo函数的关系: Frame+ f_...
defcurrentframe():"""Return the frame object for the caller's stack frame."""try:raise Exceptionexcept:return sys.exc_info()[2].tb_frame.f_backdeffindCaller(self):""" Find the stack frame of the caller so that we can note the source file name, line number and function name. """ ...
这时,可以将实现类似操作的代码封装为函数,然后在需要的地方调用该函数。
方法1 使用 sys 库 import sys sys._getframe().f_code.co_name 方法2 使用 inspect 库 ...
# Find caller from where originated the logged message. frame,depth=inspect.currentframe(),0 whileframeand(depth==0orframe.f_code.co_filename==logging.__file__): frame=frame.f_back depth+=1 logger.opt(depth=depth,exception=record.exc_info).log(level,record.getMessage()) ...
(hostname for IPv6 should be placed in brackets) # tftp://hostname # ftp://username:password@hostname # sftp://username:password@hostname[:port] # sftp-sha1://username:password@hostname[:port] # http://hostname[:port] # 2) Do not add a trailing slash at the end of file ...
返回值为object的所有成员,以(name,value)对组成的列表 inspect.ismodule(object): 是否为模块 inspect.isclass(object):是否为类 inspect.ismethod(object):是否为方法(bound method written in python) inspect.isfunction(object):是否为函数(python function, including lambda expression) inspect.isgenerator...
比如:deffoo():temp=yield1return'...',temp,True,1defget():returnfoo()if__name__=='__...
instances",type->tp_name);returnNULL;}obj=type->tp_new(type,args,kwds);obj=_Py_CheckFunction...