代码说明 导入inspect模块。 定义一个函数get_current_function_name,该函数使用inspect.currentframe()获取当前的堆栈帧,然后通过f_code.co_name属性获取函数的名字。 定义一个示例函数example_function,在该函数内部调用get_current_function_name并打印当前函数的名字。 调用example_function,查看输出的函数名字。 当你运...
importinspectdefget_current_function_name():returninspect.currentframe().f_back.f_code.co_namedefmy_function():print("当前函数名为:",get_current_function_name())my_function() 1. 2. 3. 4. 5. 6. 7. 8. 9. 在上面的代码中,我们首先导入inspect模块,然后定义了一个get_current_function_name...
defget__function_name():'''获取正在运行函数(或方法)名称'''returninspect.stack()[1][3]defyoyo():print("函数名称:%s"%get__function_name())classYoyo():defyoyoketang(self):'''# 上海-悠悠 QQ群:588402570'''print("获取当前类名称.方法名:%s.%s"%(self.__class__.__name__,get__function...
还可以使用inspect模块动态获取当前运行的函数名 import inspect def get_current_function_name(): return inspect.stack()[1][3] class MyClass: def function_one(self): print("%s.%s invoked" % (self.__class__.__name__, get_current_function_name())) if __name__ == "__main__": mycla...
defa():print sys._getframe().f_code.co_name 二、 使用inspect模块动态获取当前运行的函数名 动态获取当前运行的函数名很方便,特别是对于一些debug系统来说 importinspectdefget_current_function_name():return inspect.stack()[1][3]classMyClass:deffunction_one(self):print"%s.%s invoked"%(self.__cla...
返回值为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...
keywords=inspect.getargspec(self.__init__.__func__).keywords ifkeywords: keywords_dict=locals_dict[keywords] forkinkeywords_dict: setattr(self, k, keywords_dict[k]) classFoo(object): def__init__(self, name,**kwargv): attr_from_locals(locals()) ...
isclass inspect.EndOfBlock inspect.iscode inspect.ModuleInfo inspect.isdatadescriptor inspect.TPFLAGS_IS_ABSTRACT inspect.isframe inspect.Traceback inspect.isfunction inspect.attrgetter inspect.isgenerator inspect.classify_class_attrs inspect.isgeneratorfunction inspect.cleandoc inspect.isgetsetdescriptor ...
def my_function(): passprint("函数名称是:{}".format(my_function.__name__))```输出:```函数名称是:my_function```3. 使用`inspect`模块获取函数的名称:```pythonimport inspectdef my_function(): passfunction_name = inspect.getframeinfo(inspect.currentframe()).functionprint(function_name)```...
inspect.getfile(object) Return the name of the (text or binary) file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function. inspect.getmodule(object) Try to guess which module an object was defined in. inspect.getsourcefi...