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

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

2.获取源码

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

4.解析堆栈

一、type and members

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

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

返回值为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.isgeneratorfunction(object):是否为python生成器函数

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

inspect.istraceback(object): 是否为traceback

inspect.isframe(object):是否为frame

inspect.iscode(object):是否为code

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

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

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

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

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

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

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_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): 返回一个命名元组(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])

inspect python_python之inspect模块相关推荐

  1. inspect python_python的inspect模块

    发现python有个好用的检查模块-inspect, 查看源文件发现它提供了不少好用的方法: """ Here are some of the useful functi ...

  2. inspect python_Python之inspect模块实现获取加载模块路径的方法

    该文主要介绍如何获取模块的路径,需要申明的是这里所说的模块可以是功能实现的该模块,也可以是别的模块. 使用到的是 inspect 模块的 .getsourcefile(需要获取的模块名) 创建test ...

  3. inspect python_Python标准库inspect的具体使用方法

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

  4. inspect python_python inspect模块

    参考链接:https://blog.csdn.net/weixin_35955795/article/details/53053762 更深:https://blog.csdn.net/mldxs/a ...

  5. 移动端真机调试:chrome://inspect/#devices打开inspect后出现空白页

    问题详细 我已经配置好了usb调试: 但是当我inspect的时候,加载不了:非常自闭 问题原因 Chrome调试手机端其实是去访问那个网站,为什么要去访问那个网址呢?而不是提供本地的解决方案?我猜想 ...

  6. sqlite3修改表内容python_Python sqlite3数据库模块使用攻略

    Python作为数据科学主流语言,被广泛用于数据读存.处理.分析.建模,可以说是无所不能. 数据一般存放在本地文件或者数据库里,之前介绍过如何使用python读取本地文件,也对# PyMySQL.cx ...

  7. shutil python_python之shutil模块

    shutil:高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])(copyfileobj方法只会拷贝文件内容) 将文件内容拷贝到另 ...

  8. pickle模块 python_Python之Pickle模块

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  9. clip python_python中numpy模块下的np.clip()的用法

    Numpy 中clip函数的使用 numpy.clip(a, a_min, a_max, out=None) Clip (limit) the values in an array. Given an ...

最新文章

  1. GPT3后可考虑的方向-知识推理与决策任务及多模态的信息处理
  2. 请注意更新TensorFlow 2.0的旧代码
  3. windows mobile设置插移动卡没反应_ipad pro外接移动硬盘ipados
  4. 实验吧--web--天下武功唯快不破
  5. 手机中geetest是什么文件_安卓手机系统中各类英文文件夹的含义详解,不知道的尽快熟知!...
  6. 这样调优:让你的 IDEA 快到飞起来,效率真高!
  7. [算法模版]Link-Cut-Tree
  8. 电子商务公司的职能架构及基础岗位职能
  9. Objects.equals(a, b)
  10. 方程组的直接解法和迭代法 python_基于任务驱动的翻转课堂线上教学 ——以《解二元一次方程组复习课》为例...
  11. Android Gallery组件实现循环显示图像
  12. 改服务器的ip地址如何修改密码,服务器ip地址修改密码
  13. linux7.0 端口占用,Windows 7如何处理 80端口被占用
  14. VMware与Centos系统
  15. Senparc.Weixin.MP.Sample 配置redis服务器密码
  16. msvcr80.dll 问题
  17. 一周信创舆情观察(11.16~11.22)
  18. 【恶搞Python】Python实现QQ连续发送信息的代码,咋就说可还刑
  19. 电脑故障维修常见的故障整理,电脑小白必备!
  20. matlab R2021b 激活错误

热门文章

  1. spring源码:资源管理器Resource
  2. 等差数列划分 II - 子序列(动态规划)
  3. 1095. 山脉数组中查找目标值(三分+二分)
  4. Swap Letters CodeForces - 1215C(贪心)
  5. 计算机学院 年度工作计划,计算机教研组年度工作计划
  6. mysql denide_MYSQL 出现Error1045 access denied 的解决方法
  7. PAT_B_1055_Java(25分)
  8. excel python插件_利用 Python 插件 xlwings 读写 Excel
  9. 在用的虚拟服务器减少内存,降低虚拟服务器内存使用率
  10. linux刚重启就报资源不可用,linux系统重启网络配置