Python标准库inspect
inspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等,有几个好用的方法:
getargspec(func)
返回一个命名元组ArgSpect(args, varargs, keywords, defaults),args是函数位置参数名列表,varargs是*参数名,keywords是**参数名,defaults是默认参数值的元组。
在用__init__参数自动初始化实例属性的实践中,是用字节码对象的co_varnames属性来获取函数的位置参数名的:
1 2 3 4 5 6 7 8 9 10 11 | def attr_from_locals(locals_dict): self = locals_dict.pop( 'self' ) code = self .__init__.__func__.__code__ args = code.co_varnames[ 1 :code.co_argcount] for k in args: setattr ( self , k, locals_dict[k]) class Foo( object ): def __init__( self , name, color, num = 1 ): x = 1 attr_from_locals( locals ()) |
而当__init__方法使用**特殊参数接收任意数量的关键字参数时,上述代码是不适用的。可行的办法是使用字节码的co_flags属性来判断**参数是否存在。
函数使用*args语法来接受任意数量的位置参数时,co_flags置位0x04,使用**kwargs语法时,置位0x08,函数为一个生成器时,置位0x2000,其它位保留:
1 2 3 4 5 6 7 8 9 | >>> def foo(x, * args, * * kwargv): pass >>> foo.__code__.co_varnames ( 'x' , 'args' , 'kwargv' ) >>> foo.__code__.co_flags & 0x04 4 >>> foo.__code__.co_flags & 0x08 8 |
inspect模块的getargspec()方法正是用此判断来获取函数的特殊参数的。现在可以方便的获取__init__的**参数了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import inspect def attr_from_locals(locals_dict): self = locals_dict.pop( 'self' ) args = inspect.getargspec( self .__init__.__func__).args[ 1 :] for k in args: setattr ( self , k, locals_dict[k]) keywords = inspect.getargspec( self .__init__.__func__).keywords if keywords: keywords_dict = locals_dict[keywords] for k in keywords_dict: setattr ( self , k, keywords_dict[k]) class Foo( object ): def __init__( self , name, * * kwargv): attr_from_locals( locals ()) f = Foo( 'bar' , color = 'yellow' , num = 1 ) print f.__dict__ |
结果为:
1 | { 'color' : 'yellow' , 'num' : 1 , 'name' : 'bar' } |
对象已经正确的初始化了。
getmembers(object[, predicate])
返回一个包含对象的所有成员的(name, value)列表。返回的内容比对象的__dict__包含的内容多,源码是通过dir()实现的。
predicate是一个可选的函数参数,被此函数判断为True的成员才被返回。
getmodule(object)
返回定义对象的模块
getsource(object)
返回对象的源代码
getsourcelines(object)
返回一个元组,元组第一项为对象源代码行的列表,第二项是第一行源代码的行号
ismodule,isclass,ismethod,isfunction,isbuiltin
一系列判断对象类型的方法,大都是包装了isinstance(object, types.FunctionType)之类语句的函数。
现在可以用类型判断来返回一个类的方法了:
1 2 3 4 5 6 7 8 9 | class Foo( object ): '''Foo doc''' def __init__( self , name): self .__name = name def getname( self ): return self .__name inspect.getmembers(Foo, inspect.ismethod) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)