所谓函数签名,就是所声明函数的形式,包括函数的返回值,参数个数,参数类型,调用方式等
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能检查以下属性
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 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 |
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
这个还没完全理解,不瞎说