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等)的如下属性:

Type Attribute Description Notes
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_class class 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_code code object containing compiled function bytecode  
  func_defaults tuple of any default values for arguments  
  func_doc (same as __doc__)  
  func_globals global namespace in which this function was defined  
  func_name (same as __name__)  
generator __iter__ defined to support iteration over container  
  close raises new GeneratorExit exception inside the generator to terminate the iteration  
  gi_code code object  
  gi_frame frame object or possibly None once the generator has been exhausted  
  gi_running set to 1 when generator is executing, 0 otherwise  
  next return the next item from the container  
  send resumes the generator and “sends” a value that becomes the result of the current yield-expression  
  throw used to raise an exception inside the generator  
traceback tb_frame frame object at this level  
  tb_lasti index of last attempted instruction in bytecode  
  tb_lineno current line number in Python source code  
  tb_next next inner traceback object (called by this level)  
frame f_back next outer frame object (this frame’s caller)  
  f_builtins builtins namespace seen by this frame  
  f_code code object being executed in this frame  
  f_exc_traceback traceback if raised in this frame, or None  
  f_exc_type exception type if raised in this frame, or None  
  f_exc_value exception value if raised in this frame, or None  
  f_globals global namespace seen by this frame  
  f_lasti index of last attempted instruction in bytecode  
  f_lineno current line number in Python source code  
  f_locals local namespace seen by this frame  
  f_restricted 0 or 1 if frame is in restricted execution mode  
  f_trace tracing function for this frame, or None  
code co_argcount number of arguments (not including * or ** args)  
  co_code string of raw compiled bytecode  
  co_consts tuple of constants used in the bytecode  
  co_filename name of file in which this code object was created  
  co_firstlineno number of first line in Python source code  
  co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg |8=**arg  
  co_lnotab encoded mapping of line numbers to bytecode indices  
  co_name name with which this code object was defined  
  co_names tuple of names of local variables  
  co_nlocals number of local variables  
  co_stacksize virtual machine stack space required  
  co_varnames tuple 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模块相关推荐

  1. python inspect模块解析

    来源:https://my.oschina.net/taisha/blog/55597 inspect模块主要提供了四种用处: (1) 对是否是模块,框架,函数等进行类型检查. (2) 获取源码 (3 ...

  2. python inspect模块

    inspect模块的四种用处: 1)对是否是模块,框架,函数等进行类型检查 2)获取源码 3)获取类或函数的参数的信息 4)解析堆栈. inspect.stack()[1][3] #当前运行的函数的函 ...

  3. Python语法学习记录(24):inspect模块介绍及常用使用方式

    1.简述 获取函数签名对象. 函数签名包含了一个函数的信息,包括函数名.它的参数类型.它所在的类和名称空间及其他信息). 2.基本用法 inspect模块主要提供了四种用处: 1.对是否是模块.框架. ...

  4. Python检查模块

    Python检查模块 (Python inspect module) Python inspect module is a very useful module which is used to in ...

  5. Python自省(反射) 与 inspect 模块

    Python 自省指南:https://www.ibm.com/developerworks/cn/linux/l-pyint/ From:https://my.oschina.net/taisha/ ...

  6. python查看模块功能_Python进阶之inspect模块使用详解

    前几篇内容我们详细探讨了如何从Python中获取帮助信息: 前情回顾 1.查看模块.类提供了哪些接口: 需要帮助吗?dir函数的孪生兄弟,Python中魔法方法__dir__详解 2.查看对象内部属性 ...

  7. python 学习汇总46:inspect 模块简介(入门基础 tcy)

    inspect 模块 2018/11/17 inspect模块主要提供了四种用处:1.对是否是模块.框架.函数进行类型检查2.获取源码3.获取类或者函数的参数信息4.解析堆栈 一.type and m ...

  8. python基础:inspect模块各函数的用法

    目录 前言 一.inspect模块总览 1.获取成员与判断 2.获取源代码 3.类与函数 4.调用栈 二.inspect模块方法的使用 1.getmembers 2.getcomments.getdo ...

  9. python中inspect模块用法详解

    获取函数签名对象.函数签名包含了一个函数的信息,包括函数名.它的参数类型.它所在的类和名称空间及其他信息). inspect模块主要提供了四种用处: 对是否是模块.框架.函数进行类型检查 获取源码 获 ...

  10. inspect模块---检查活动对象

    [inspect]模块提供了一些有用的函数来帮助获取有关活动对象(如模块,类,方法,函数,跟踪,框架对象和代码对象)的信息.例如,它可以帮助您检查类的内容,检索方法的源代码,提取和格式化函数的参数列表 ...

最新文章

  1. Coreseek:indexer crashed神秘
  2. C#中创建DLL(动态链接库)及其使用
  3. 带字的图片如何转换成可编辑的文字?
  4. struts2 手动验证和框架验证
  5. ZooKeeper与Eureka的区别
  6. rxjs里的Observable对象和map配合的一个用法
  7. oracle之基本的过滤和排序数据
  8. mysql linux设置密码_Linux下第一次使用MySQL数据库,设置密码
  9. python标准函数什么意思_python中quote函数是什么意思,怎么用
  10. 解决微信小程序要求TLS版本不低于1.2问题
  11. 【Sencha Toucha】Sencha Touch ExtJs 给 Button 添加图片
  12. 【GMSK】研究PCM/FM和GMSK的调制和解调方法
  13. 百宝云网络验证对接+脚本更新功能(源码)
  14. 七大江河水系--黄河(一)
  15. Js 实现十六进制颜色值和RGB颜色值转换整理
  16. 自动控制原理--线性系统的微分方程
  17. 用endnote x9在Word 2016中插入参考文献到特定位置
  18. ubuntu系统(二):ibus拼音将繁体中文改为简体中文
  19. 版本号Alpha、Beta、RC、Candidate、Stable分别代表什么含义?
  20. Ribbon--概述

热门文章

  1. 网络实名制离我们越来越近
  2. 微信小程序播放音乐的方法中的两种方法
  3. 愿世间项目再无node-sass
  4. 微信公众号开发redirect_uri 参数错误 的解决办法,Oauth2授权重定向域名参数错误解决办法
  5. android 手机资源获取失败,安卓root权限获取失败原因及解决办法
  6. Vue js 实现点击页面空白处隐藏指定div
  7. 技术动态 | GML如何做药物发现?奥尔胡斯大学最新《知识增强图机器学习在药物发现中的应用》综述...
  8. R语言 多元线性回归 研究年龄、身高、体重的关系
  9. 常见的损失函数(代价函数)
  10. JavaSE从头再来(一):面向对象、常用API