python--inspect模块

python

inspect模块主要提供了四种用处:

  1.对是否是模块、框架、函数进行类型检查

  2.获取源码

  3.获取类或者函数的参数信息

  4.解析堆栈

一、type and members

1. inspect.getmembers(object[, predicate])

第二个参数通常可以根据需要调用如下16个方法;

返回值为object的所有成员,以(name,value)对组成的列表

  1. inspect.ismodule(object): 是否为模块

  2. inspect.isclass(object):是否为类

  3. inspect.ismethod(object):是否为方法(bound method written in python

  4. inspect.isfunction(object):是否为函数(python function, including lambda expression)

  5. inspect.isgeneratorfunction(object):是否为python生成器函数

  6. inspect.isgenerator(object):是否为生成器

  7. inspect.istraceback(object): 是否为traceback

  8. inspect.isframe(object):是否为frame

  9. inspect.iscode(object):是否为code

  10. inspect.isbuiltin(object):是否为built-in函数或built-in方法

  11. inspect.isroutine(object):是否为用户自定义或者built-in函数或方法

  12. inspect.isabstract(object):是否为抽象基类

  13. inspect.ismethoddescriptor(object):是否为方法标识符

  14. inspect.isdatadescriptor(object):是否为数字标识符,数字标识符有__get__ 和__set__属性; 通常也有__name__和__doc__属性

  15. inspect.isgetsetdescriptor(object):是否为getset descriptor

  16. inspect.ismemberdescriptor(object):是否为member descriptor

inspect的getmembers()方法可以获取对象(module、class、method等)的如下属性: 

TypeAttributeDescriptionNotes
module__doc__documentation string 
 __file__filename (missing for built-in modules) 
class__doc__documentation string 
 __module__name of module in which this class was defined 
method__doc__documentation string 
 __name__name with which this method was defined 
 im_classclass object that asked for this method(1)
 im_func or __func__function object containing implementation of method 
 im_self or __self__instance to which this method is bound, or None

 
function__doc__documentation string 
 __name__name with which this function was defined 
 func_codecode object containing compiled function bytecode

 
 func_defaultstuple of any default values for arguments 
 func_doc(same as __doc__) 
 func_globalsglobal namespace in which this function was defined 
 func_name(same as __name__) 
generator__iter__defined to support iteration over container 
 closeraises new GeneratorExit exception inside the generator to terminate the iteration 
 gi_codecode object 
 gi_frameframe object or possibly None once the generator has been exhausted 
 gi_runningset to 1 when generator is executing, 0 otherwise 
 nextreturn the next item from the container 
 sendresumes the generator and “sends” a value that becomes the result of the current yield-expression 
 throwused to raise an exception inside the generator 
tracebacktb_frameframe object at this level 
 tb_lastiindex of last attempted instruction in bytecode 
 tb_linenocurrent line number in Python source code 
 tb_nextnext inner traceback object (called by this level) 
framef_backnext outer frame object (this frame’s caller) 
 f_builtinsbuiltins namespace seen by this frame 
 f_codecode object being executed in this frame 
 f_exc_tracebacktraceback if raised in this frame, or None

 
 f_exc_typeexception type if raised in this frame, or None

 
 f_exc_valueexception value if raised in this frame, or None

 
 f_globalsglobal namespace seen by this frame 
 f_lastiindex of last attempted instruction in bytecode 
 f_linenocurrent line number in Python source code 
 f_localslocal namespace seen by this frame 
 f_restricted0 or 1 if frame is in restricted execution mode 
 f_tracetracing function for this frame, or None

 
codeco_argcountnumber of arguments (not including * or ** args) 
 co_codestring of raw compiled bytecode 
 co_conststuple of constants used in the bytecode 
 co_filenamename of file in which this code object was created 
 co_firstlinenonumber of first line in Python source code 
 co_flagsbitmap: 1=optimized | 2=newlocals | 4=*arg |8=**arg 
 co_lnotabencoded mapping of line numbers to bytecode indices 
 co_namename with which this code object was defined 
 co_namestuple of names of local variables 
 co_nlocalsnumber of local variables 
 co_stacksizevirtual machine stack space required 
 co_varnamestuple of names of arguments and local variables 
builtin__doc__documentation string 
 __name__original name of this function or method 
 __self__instance to which a method is bound, or None

 

2. inspect.getmoduleinfo(path): 返回一个命名元组<named tuple>(name, suffix, mode, module_type)

  name:模块名(不包括其所在的package)

      suffix:

      mode:open()方法的模式,如:'r', 'a'等

      module_type: 整数,代表了模块的类型

3. inspect.getmodulename(path):根据path返回模块名(不包括其所在的package)

二、Retrieving source code

1. inspect.getdoc(object): 获取object的documentation信息

2. inspect.getcomments(object)

3. inspect.getfile(object): 返回对象的文件名

4. inspect.getmodule(object):返回object所属的模块名

5. inspect.getsourcefile(object): 返回object的python源文件名;object不能使built-in的module, class, mothod

6. inspect.getsourcelines(object):返回object的python源文件代码的内容,行号+代码行

7. inspect.getsource(object):以string形式返回object的源代码

8. inspect.cleandoc(doc):

三、class and functions

1. inspect.getclasstree(classes[, unique])

2. inspect.getargspec(func)

3. inspect.getargvalues(frame)

4. inspect.formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue, join])

5. inspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue, join])

6. inspect.getmro(cls): 元组形式返回cls类的基类(包括cls类),以method resolution顺序;通常cls类为元素的第一个元素

7. inspect.getcallargs(func[, *args][, **kwds]):将args和kwds参数到绑定到为func的参数名;对bound方法,也绑定第一个参数(通常为self)到相应的实例;返回字典,对应参数名及其值;

>>> from inspect import getcallargs

>>> def f(a, b=1, *pos, **named):

... pass

>>> getcallargs(f, 1, 2, 3)

{'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}

>>> getcallargs(f, a=2, x=4)

{'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}

>>> getcallargs(f)

Traceback (most recent call last):

...

TypeError: f() takes at least 1 argument (0 given)

四、The interpreter stack

1. inspect.getframeinfo(frame[, context])

2. inspect.getouterframes(frame[, context])

3. inspect.getinnerframes(traceback[, context])

4. inspect.currentframe()

5. inspect.stack([context])

6. inspect.trace([context])

以上是 python--inspect模块 的全部内容, 来源链接: utcz.com/z/387673.html

回到顶部