python边用边总结(九)python的inspect

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

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

https://docs.python.org/2/library/inspect.html

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能检查以下属性

TypeAttributeDescription
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_classclass 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_codecode object containing compiled function bytecode
 func_defaultstuple of any default values for arguments
 func_doc(same as __doc__)
 func_globalsglobal namespace in which this function was defined
 func_name(same as __name__)
generator__iter__defined to support iteration over container
 closeraises new GeneratorExit exception inside the generator to terminate the iteration
 gi_codecode object
 gi_frameframe object or possibly None once the generator has been exhausted
 gi_runningset to 1 when generator is executing, 0 otherwise
 nextreturn the next item from the container
 sendresumes the generator and “sends” a value that becomes the result of the current yield-expression
 throwused to raise an exception inside the generator
tracebacktb_frameframe object at this level
 tb_lastiindex of last attempted instruction in bytecode
 tb_linenocurrent line number in Python source code
 tb_nextnext inner traceback object (called by this level)
framef_backnext outer frame object (this frame’s caller)
 f_builtinsbuiltins namespace seen by this frame
 f_codecode object being executed in this frame
 f_exc_tracebacktraceback if raised in this frame, or None
 f_exc_typeexception type if raised in this frame, or None
 f_exc_valueexception value if raised in this frame, or None
 f_globalsglobal namespace seen by this frame
 f_lastiindex of last attempted instruction in bytecode
 f_linenocurrent line number in Python source code
 f_localslocal namespace seen by this frame
 f_restricted0 or 1 if frame is in restricted execution mode
 f_tracetracing function for this frame, or None
codeco_argcountnumber of arguments (not including * or ** args)
 co_codestring of raw compiled bytecode
 co_conststuple of constants used in the bytecode
 co_filenamename of file in which this code object was created
 co_firstlinenonumber of first line in Python source code
 co_flagsbitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
 co_lnotabencoded mapping of line numbers to bytecode indices
 co_namename with which this code object was defined
 co_namestuple of names of local variables
 co_nlocalsnumber of local variables
 co_stacksizevirtual machine stack space required
 co_varnamestuple 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 np

getfile(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

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值