Python标准库inspect的具体使用方法

时间:2019-04-11
本文章向大家介绍Python标准库inspect的具体使用方法,主要包括Python标准库inspect的具体使用方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

inspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等,有几个好用的方法:

Doc:这样写到

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.

这个模块是针对模块,类,方法,功能等对象提供些有用的方法。

getargspec(func)

返回一个命名元组ArgSpect(args, varargs, keywords, defaults),args是函数位置参数名列表,varargs是*参数名,keywords是**参数名,defaults是默认参数值的元组。

在用__init__参数自动初始化实例属性的实践中,是用字节码对象的co_varnames属性来获取函数的位置参数名的:

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,其它位保留:

>>> 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__的**参数了:

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__

结果为:

{'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)之类语句的函数。

现在可以用类型判断来返回一个类的方法了:

class Foo(object):
 '''Foo doc'''
 def __init__(self, name):
  self.__name = name
 def getname(self):
  return self.__name
inspect.getmembers(Foo, inspect.ismethod)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。