python--inspect模块
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等)的如下属性:
| 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模块的更多相关文章
- python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性
python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性 inspect import inspect def fun(): pass inspect.ism ...
- python——inspect模块
inspect模块常用功能 import inspect # 导入inspect模块 inspect.isfunction(fn) # 检测fn是不是函数 inspect.isgenerator((x ...
- python之inspect模块
inspect模块主要提供了四种用处: 1.对是否是模块.框架.函数进行类型检查 2.获取源码 3.获取类或者函数的参数信息 4.解析堆栈 回到顶部 一.type and members 1. i ...
- 14 - 函数参数检测-inspect模块
目录 1 python类型注解 2 函数定义的弊端 3 函数文档 4 函数注解 4.1 annotation属性 5 inspect模块 5.1 常用方法 5.2 signature类 5.3 par ...
- inspect模块---检查活动对象
inspect模块提供了一些有用的函数来帮助获取有关活动对象(如模块,类,方法,函数,跟踪,框架对象和代码对象)的信息.例如,它可以帮助您检查类的内容,检索方法的源代码,提取和格式化函数的参数列表,或 ...
- inspect模块详解
inspect模块主要提供了四种用处: (1).对是否是模块,框架,函数等进行类型检查. (2).获取源码 (3).获取类或函数的参数的信息 (4).解析堆栈 使用inspect模块可以提供自省功能, ...
- inspect模块的使用
一.介绍 inspect模块主要的四种用处: 1.对是否是模块.框架.函数等进行类型检测 2.获取源码 3.获取类或函数的参数信息 4.解析堆栈 二.使用 只写了2个自己用到的方法,方法太用,http ...
- python inspect库
一.介绍 inspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等. inspect模块主要提供了四种用处: 对是否是模块.框架.函数进行 ...
- Python标准模块--threading
1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...
- Python的模块引用和查找路径
模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译型的语言,比如C#中 ...
随机推荐
- Android 混淆打包
有些时候我们希望我们自己的apk包不能被别人反编译而获取自己的源代码.这就需要我们通过Android提供的混淆打包技术来完成. 一.没有引用外部包的情况: 这种情况下代码混淆的方式相对简单: 1)只需 ...
- HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【转】深入理解C++的动态绑定和静态绑定 & 不要重定义虚函数中的默认参数
为了支持c++的多态性,才用了动态绑定和静态绑定.理解他们的区别有助于更好的理解多态性,以及在编程的过程中避免犯错误.需要理解四个名词:1.对象的静态类型:对象在声明时采用的类型.是在编译期确定的.2 ...
- Jquery的Ajax中contentType和dataType的区别(转载)
上代码 $.ajax({ type: httpMethod, cache: false, contentType: "application/json; charset=utf-8" ...
- 基于Python自动上传包到nexus仓库
1.设计思路 用户通过excel表格的形式填写包的信息,并将包一起发送给负责人 2.代码实现 #coding:utf8 import os import xlrd def GetData(fileNa ...
- android studio更新gradle失败的解决办法-转
android studio中每次自动更新gradle时速度实在太慢因为gradle服务器比较慢,所以更新gradle会比较慢,建议先下载下来,然后手动添加到gradle的下载目录,提升速度. 使用下 ...
- MFC如何为程序添加图标
1.找几幅Ico格式的图片,可以在电脑中查找.ico一般都会找到.然后将ico文件放在工程目录下的res文件夹下. 2.点击菜单栏->编辑->添加资源->导入,选择res文件夹中将要 ...
- [Deep-Learning-with-Python]基于Keras的房价预测
预测房价:回归问题 回归问题预测结果为连续值,而不是离散的类别. 波士顿房价数据集 通过20世纪70年代波士顿郊区房价数据集,预测平均房价:数据集的特征包括犯罪率.税率等信息.数据集只有506条记录, ...
- GIT命令基本使用
记录摘选自廖雪峰的官方网站归纳总结 1.centos下安装git [root@cdw-lj ~]# yum install git 2.配置用户名以及邮箱 [root@cdw-lj opt]# git ...
- CS50.1
1,GUI,graphical user interface,图形用户界面 2.VB,visual basic,微软开发的一种程序语言 3,BIT,binary digit 比特 4,byte 5,8 ...