python边用边总结(九)python的inspect
发布日期:2021-06-29 06:03:25 浏览次数:2 分类:技术文章

本文共 5035 字,大约阅读时间需要 16 分钟。

所谓函数签名,就是所声明函数的形式,包括函数的返回值,参数个数,参数类型,调用方式等

python里面把函数看成对象,内置库inspect可以查看函数对象,今天记录以下inspect这个库,以便以后使用

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

inspect模块提供了几个有用的函数来帮助获取有关活动对象的信息,例如模块,类,方法,函数,回溯,框架对象和代码对象。 例如,它可以帮助您检查类的内容,检索方法的源代码,提取和格式化函数的参数列表,或获取显示详细回溯所需的所有信息。

There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.

inspect提供了四个类型的服务:类型检查,源码获取,类和函数监测,检查翻译栈

  • Types and members

inspect能检查以下属性

Type Attribute Description
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
  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
  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

getmembers(object[,predicate]):Return all teh members of an object in a list of (name,value) pairs sorted by name.

getmodule(moudle_name):

ismoudle(object):

ismethod(object):

isclass(object):

isfunction(object):

isgeneratorfunction(object):Return true if the object is a Python generator function

isframe(object):

iscode(object):

isbuiltin(object):Return true if the object is a built-in function or a bound built-in method.

isroutine(object):

isabstract(object):

  • Retrieving source code

getdoc(object):Get the documentation string for an object, cleaned up with cleandoc().

getcomments(object):返回comments

getfile(object):返回对象所在文件

from inspect import *import numpy as npgetfile(np)# 输出的是numpy的文件路径

getmodule(object):

getsourcefile(object):

getsourcelines(object):

getsource(object):

cleandoc(doc):

  • Classes and functions

getclasstree(classes[,unique])

getargspec(func):这个函数用处比较大,get the names and default values of a python function's arguments.A tuple of four things is returned:(args,varargs,keywords,defaults)

getargvalues(frame):

fromatargspec():

formatargvalues():

getmro(cls):Return a tuple of class cls's base classes,including cls,in method resolution order

getcallargs():

  • The interpreter stack

这个还没完全理解,不瞎说

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:https://blog.csdn.net/zhouzhouasishuijiao/article/details/85055176 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python边写边总结(十)Numba文档阅读记录
下一篇:python边写边总结(八)python的装饰器

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月23日 17时53分31秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

小白入门网络安全,需要学习哪些内容? 2019-04-29
如何找回丢失的比特币 2019-04-29
日本交易所Zaif因黑客攻击损失近6000万美元 2019-04-29
以太坊智能合约如何运作? 2019-04-29
赛迪全球公链指数第5期名单:EOS、ETH排名不变 比特股首进前3 2019-04-29
能依靠0.21个比特币生存21天吗 2019-04-29
比特币核心发现网络漏洞CVE-2018-17144 社区敦促所有节点尽快升级补丁 2019-04-29
泰达币对比特币价格的影响不具有统计意义 2019-04-29
Atitit nlp自然语言处理类库(java python nodejs c#net) 目录 1.1. Python snownlp 1 1.2. NLP.js一个nodejs/javascri 2019-04-29
Atitit nlp用到的技术与功能自然语言处理 v3 t99.docx Atitit nlp用到的技术与常见类库 目录 1. 常用的技术 1 1.1. 语言处理基础技术 分词 相似度等 1 1.2 2019-04-29
Atitit 2000drmmr v2 t05.docx Atitit 2000drmmr v1 t99 2000.1.26 hs,yweywe jla n dashw ner kl .na ni 2019-04-29
常见方案 目录 1. 发现目前 WEB 上主流的视频直播方案有 HLS 和 RTMP, 1 2. 实现直播的方法有很多,但是常用的,就这几个。 3个直播协议:rtmp、rtsp、hls。 和三个端: 2019-04-29
Atitit 直播问题总结ffmpeg 目录 1.1. 屏幕太大,可以使用-s调整分辨率 1 1.2. Full size 1 1.3. 流畅度调整 1 2. 1 2.1. 没有录音 1 2.2. 2019-04-29
paip.索引优化---sql distict—order by 法 2019-04-29
paip.输入法编程---带ord gudin去重复- 2019-04-29
paip.输入法编程---增加码表类型 2019-04-29
paip.提升性能--- mysql 建立索引 删除索引 很慢的解决. 2019-04-29
paip.输入法编程---智能动态上屏码儿长调整--.txt 2019-04-29
Atitit sumdoc t0 final index D:\BaiduNetdiskDownload\sumdoc t0 final\sumdoc t0 wps cld bek D:\Baid 2019-04-29
Atitit sumdoc t0 final index D:\BaiduNetdiskDownload\sumdoc t0 final\sumdoc t0 wps cld bek D:\Baid 2019-04-29