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

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])

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/245536.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTML打开网页拒绝访问,192.168.1.1拒绝访问怎么办?

问:为什么设置路由器时,在浏览器中输入192.168.1.1,结果显示拒绝访问,这个问题怎么解决?答:如果是在设置路由器的时候,登录192.168.1.1被拒绝访问,多半是你自己操作有问题导致的&…

python中goto的用法_python3里用goto

python里用goto也是小Pa最近做的项目里的一个需求。python不像C有自带的goto, 需要用额外的包,目前为止,小pa只看到2个goto的包: 这2个小Pa都下载试用过,goto因为开发的时候比较早,对于python3的支持不太好,不推荐使用…

delphi打印html文件路径,Delphi获取文件名、不带扩展名文件名、文件所在路径、上级文件夹路径的方法...

1.获取不带扩展名的文件名方法,利用ChangeFileExt函数修改传入参数的扩展为空,并不会对文件本身产生变更。ChangeFileExt(ExtractFileName(‘D:\KK\Test\123.txt‘),‘‘); //返回 1232.获取上级文件夹路径的方法。ExtractFileDir(‘D:\KK\Test\‘)‘..‘…

gitlab git clone 输入密码_gitlab1:部署gitlab

1、配置yum源vim /etc/yum.repos.d/gitlab-ce.repo复制以下内容:[gitlab-ce]nameGitlab CE Repositorybaseurlhttps://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/gpgcheck0enabled12、更新本地yum缓存sudo yum makecache3、安装GitLab社区版sudo y…

计算机专业英语第五章ppt,计算机专业英语第五章.ppt

计算机专业英语第五章Background The Internet protocols are the worlds most popular open-system (nonproprietary) protocol suite because they can be used to communicate across any set of interconnected networks and are equally well suited for LAN and WAN comm…

python播放在线音乐_Python实现在线音乐播放器

最近这几天,学习了一下python,对于爬虫比较感兴趣,就做了一个简单的爬虫项目,使用Python的库Tkinsert做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音乐的一个接口,通过re…

golang如何打印float64的整数部分_2020-08-10:如何不用加减乘除求浮点数的2倍值?...

福哥答案2020-08-10:浮点数符号位阶码尾数,阶码加1就是浮点数的2倍值。代码用golang编写,如下:package test33_addimport ( "fmt" "math" "testing")/*//https://www.bbsmax.com/A/6pdDX7…

五年级数学上册用计算机探索规律,人教版小学五年级数学上册《用计算器探索规律》课后反思...

当前,新课程改革强调学生学习方式的转变.高效课堂是课程改革过程中有效学习方式之一.在高效课堂中,孩子们能发挥自己潜能、展示自己的才能,提高了孩子们的学习兴趣.如何让高效课堂焕发光彩能?一、合理分组,恰当分工合理分组是高效课堂顺利进行的前提.在以前的学习过…

mysql varchar 非空判断_工资从1万到3万,你还差mysql数据库优化之系列三

查询性能的优化优化查询分析的步骤:1.应用查询是否检索超过需要的数据2.mysql服务器是否在分析超过需要的数据正确使用索引:1.like语句操作一般不使用%或_开头例如: select * from tableName where name like %cn;只能使用like aaa%;2.组合索引例如索引index index_name (a, b,…

etl数据抽取工具_数据同步工具ETL、ELT傻傻分不清楚?3分钟看懂两者区别

什么是数据同步工具(ETL、ELT)数据同步工具ETL或者ELT的作用是将业务系统的数据经过抽取、清洗转换之后加载到数据仓库的过程,目的是将企业中的分散、零乱、标准不统一的数据整合到一起,为企业的决策提供分析依据。数据同步是大数据项目重要的一个环节。…

浙江等高等学校计算机,2010年浙江省高等学校计算机等级考试

2010年上半年浙江省高等学校计算机等级考试二级C程序设计试卷一、程序阅读与填空(24小题,每小题3分同,共72分)1.阅读下列程序说明和程序,在每小题提供的若干可选答案中,挑选一个正确答案。【程序说明】输入一个正整数&…

oracle数据库查表_Oracle面试问题-技术篇

这也许是你一直期待的文章,在关注这部分技术问题的同时,请务必阅读有关面试中有关个人的问题和解答。和猎萝卜小编来一起了解。这里的回答并不是十分全面,这些问题可以通过多个角度来进行解释,也许你不必在面试过程中给出完全详尽…

uniapp怎么调起摄像头拍视频_抖音视频怎么拍?我们总结了10个手机视频拍摄小技巧...

抖音的很多功能与小咖秀类似,但不同的是,抖音用户可以通过视频拍摄的快慢、视频编辑和特效等技术让作品更具创造性,而不是简单地对嘴型。抖音短视频的10个拍摄技巧,帮助你方便、快捷地制作出更加优质的短视频内容。1.远程控制暂停更方便抖音视频怎么拍?在拍摄时,如果…

计算机的两个基本能力是存储程序,【2012年职称计算机模拟题(55)】- 环球网校...

15.简述计算机的存储程序原理$lesson$答:计算机的工作方式取决于它的两个基本能力,A.是能够存储程序;B.是能够自动执行程序。计算机利用存储器(内存)来存放所要执行的程序,而CPU可以依次从存储器中取出程序的每一条指令,并加以分析…

jdk 安装_Jdk 安装使用教程

java 程序员的第一个程序 hello worldpublic class MyFirstJavaProgram {public static void main(String []args) {System.out.println("Hello World");}}1、下载jdk连接地址:https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_o…

win10计算机管理字体糊,win10字体模糊如何解决

win10系统是一款优秀的消费级别的系统,深受大家广泛好评。但是有些网友在使用时也会出现一些问题,比如win10字体模糊。接下来,我就给大家介绍一下解决win10字体模糊的解决方法,赶紧来瞧瞧吧近来有不少网友询问win10字体模糊的解决…

蓝字冲销是什么意思_梦见上学 做梦梦到上学是什么意思 梦到上学有哪些预兆...

点击上方蓝字关注我们 查看更多梦见上学是什么意思 做梦梦到上学是什么意思 梦到上学有哪些预兆梦见上学 做梦梦到念书是什么意思 梦见上学代表什么意思预兆梦见上学,吉兆,生活会幸福快乐。梦见上学,可能是因为你近期的生活工作压力导致的&am…

ffmpeg如何在结尾添加帧_一种“视频帧对齐”的测试方案实践

点击蓝字?关注【测试先锋】,不再迷路!一起成为互联网测试精英,前瞻测试技术~导语全参考清晰度测算的时候,输入两个视频帧序列,但是视频帧序列没有对齐,怎么知道丢了哪帧?又怎么知道…

2021盐城中考有计算机考试吗,2021盐城中考总分满分是多少?各科目分值设置

2021盐城中考总分满分是多少?各科目分值设置三、考试科目盐城市2021年初中毕业与升学考试(简称“中考”)模式为“学业水平考试实验实践技能考查综合素质评价”。学业水平考试和实验实践技能考查的评价标准由市教科院制定发布。1. 学业水平考试2021年中考学业水平考试…

java mongo分组统计_探秘 Dubbo 的度量统计基础设施 - Dubbo Metrics

对服务进行实时监控,了解服务当前的运行指标和健康状态,是微服务体系中不可或缺的环节。Metrics 作为微服务的重要组件,为服务的监控提供了全面的数据基础。近日,Dubbo Metrics 发布了2.0.1版本,本文将为您探秘 Dubbo …