python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python的inspect模块

python标准库中inspect模块的简单说明

作者:阿常呓语

这篇文章主要介绍了python标准库中inspect模块的简单介绍,inspect模块提供了几个有用的函数来帮助获取有关活动对象的信息,例如模块,类,方法,函数,回溯,框架对象和代码对象,需要的朋友可以参考下

inspect 模块作用

(1).对是否是模块,框架,函数等进行类型检查。

(2).获取源码

(3).获取类或函数的参数的信息

以下是官方文档说明:

inspect模块提供了几个有用的函数来帮助获取有关活动对象的信息,例如模块,类,方法,函数,回溯,框架对象和代码对象。

例如,它可以帮助您检查类的内容,检索方法的源代码,提取和格式化函数的参数列表,或获取显示详细回溯所需的所有信息。

inspect

可以 比较轻松 获取 函数的参数, 通过 signature(foo).bind(xxx,xxx) 就可以了.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import inspect
def foo(a, b='ham', *args):
    pass
def test(a, *, b):
    print(a,b)
if __name__ == '__main__':
    ba = inspect.signature(foo).bind(a='spam', b='frank')
    ba.apply_defaults()
    print(ba.arguments)  # ordereddict
# OrderedDict([('a', 'spam'), ('b', 'frank'), ('args', ())])
import  inspect 
def foo(a,b,c,*args,**kw):pass
# 拿到函数 签名 
sig  = inspect.signature(foo)
str(sig)
'(a, b, c, *args, **kw)'
## 绑定部分参数,  返回一个 BoundArguments 对象 
sig.bind_partial('frank','165cm')
<BoundArguments (a='frank', b='165cm')>
ba =sig.bind_partial('frank','165cm')
## 对于丢失的参数,可以设置默认值
ba.apply_defaults()
ba.arguments
OrderedDict([('a', 'frank'), ('b', '165cm'), ('args', ()), ('kw', {})])
## signature  还有一个方法  bind
import  inspect 
def foo(a,b,c,*args,**kw):pass
sig =  inspect.signature(foo)
ba = sig.bind('frank','165cm','hello')
ba
<BoundArguments (a='frank', b='165cm', c='hello')>
ba.arguments
OrderedDict([('a', 'frank'), ('b', '165cm'), ('c', 'hello')])
# 指定默认值 
ba.apply_defaults()
ba.arguments
OrderedDict([('a', 'frank'), ('b', '165cm'), ('c', 'hello'), ('args', ()), ('kw', {})])

bind 和 bind_partial 区别

绑定参数的,如果 用 bind 需要要所有的位置参数 都要绑定. bind_partial 可以部分进行绑定.

import  inspect 
def foo(a,b,c,*args,**kw):pass
sig =  inspect.signature(foo)
sig
<Signature (a, b, c, *args, **kw)>
sig
<Signature (a, b, c, *args, **kw)>
sig.bind('frank','165cm')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 2968, in bind
    return args[0]._bind(args[1:], kwargs)
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 2883, in _bind
    raise TypeError(msg) from None
TypeError: missing a required argument: 'c'
不支持 部分绑定参数, 所以会报错, 可以使用 sig.bind_partial
sig.bind_partial('frank','165cm')
<BoundArguments (a='frank', b='165cm')>

The args and kwargs properties can be used to invoke functions:

signature.bind() 返回一个 Boundarguments 对象. 可以通过 args , kwargs 拿到函数的所有参数.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import inspect
def test(a, *, b):
    print(a, b)
if __name__ == "__main__":
    sig = inspect.signature(test)
    ba = sig.bind(10, b=20)
    test(*ba.args, **ba.kwargs)  # 10 20
# The args and kwargs properties can be used to invoke functions  

实用方法

用来判断 函数,类,实例方法, 内建方法, function or method等…

def ismodule(object):
def isclass(object):
def ismethod(object):
def ismethoddescriptor(object):
def isfunction(object):
def isbuiltin(object):
def isabstract(object):
def ismethoddescriptor(object):
def isroutine(object):
    """Return true if the object is any kind of function or method."""

举个例子

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: Frank 
@contact: frank.chang@shoufuyou.com
@file: test_inspect.py
@time: 2018/5/14 下午1:24
"""
from inspect import getmembers, ismethod, isclass, isfunction
def foo(): pass
class Cat(object):
    def __init__(self, name='kitty'):
        self.name = name
    def sayHi(self):
        print(self.name + '  says Hi!')
    def fun1(self):
        print('fun1 is running')
if __name__ == '__main__':
    cat = Cat('frank')
    cat.sayHi()
    print(isclass(cat))  # False
    print(isclass(Cat))  # True
    print(ismethod(cat.sayHi))  # True
    print(isfunction(cat.sayHi))  # False
    print(isfunction(foo))  # true
    print(ismethod(cat.fun1))  # True

getmemory

if __name__ == '__main__':
    cat = Cat('frank')
    # print(getmembers(cat))
    for member in getmembers(cat):
        print(member)   

# 结果如下
# ('__class__', <class '__main__.Cat'>)
# ('__delattr__', <method-wrapper '__delattr__' of Cat object at 0x10b213dd8>)
# ('__dict__', {'name': 'frank'})
# ('__dir__', <built-in method __dir__ of Cat object at 0x10b213dd8>)
# ('__doc__', None)
# ('__eq__', <method-wrapper '__eq__' of Cat object at 0x10b213dd8>)
# ('__format__', <built-in method __format__ of Cat object at 0x10b213dd8>)
# ('__ge__', <method-wrapper '__ge__' of Cat object at 0x10b213dd8>)
# ('__getattribute__', <method-wrapper '__getattribute__' of Cat object at 0x10b213dd8>)
# ('__gt__', <method-wrapper '__gt__' of Cat object at 0x10b213dd8>)
# ('__hash__', <method-wrapper '__hash__' of Cat object at 0x10b213dd8>)
# ('__init__', <bound method Cat.__init__ of <__main__.Cat object at 0x10b213dd8>>)
# ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x7fc8afc24358>)
# ('__le__', <method-wrapper '__le__' of Cat object at 0x10b213dd8>)
# ('__lt__', <method-wrapper '__lt__' of Cat object at 0x10b213dd8>)
# ('__module__', '__main__')
# ('__ne__', <method-wrapper '__ne__' of Cat object at 0x10b213dd8>)
# ('__new__', <built-in method __new__ of type object at 0x10afc86c0>)
# ('__reduce__', <built-in method __reduce__ of Cat object at 0x10b213dd8>)
# ('__reduce_ex__', <built-in method __reduce_ex__ of Cat object at 0x10b213dd8>)
# ('__repr__', <method-wrapper '__repr__' of Cat object at 0x10b213dd8>)
# ('__setattr__', <method-wrapper '__setattr__' of Cat object at 0x10b213dd8>)
# ('__sizeof__', <built-in method __sizeof__ of Cat object at 0x10b213dd8>)
# ('__str__', <method-wrapper '__str__' of Cat object at 0x10b213dd8>)
# ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x7fc8afc24358>)
# ('__weakref__', None)
# ('fun1', <bound method Cat.fun1 of <__main__.Cat object at 0x10b213dd8>>)
# ('name', 'frank')
# ('sayHi', <bound method Cat.sayHi of <__main__.Cat object at 0x10b213dd8>>)

总结

当然inspect 有很多方法, 比如 获取源码这个,这个就我没有太用过

经常用的可能就是我例子给出的,判断类型,以及拿到函数的参数,用inspect模块非常方便,甚至 可以通过这个模块,拿到函数的参数,写一个装饰器,对函数参数进行检查

到此这篇关于python标准库中inspect模块的简单说明的文章就介绍到这了,更多相关python的inspect模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文