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

 更新时间:2023年08月02日 11:06:10   作者:阿常呓语  
这篇文章主要介绍了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模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 举例详解Python中threading模块的几个常用方法

    举例详解Python中threading模块的几个常用方法

    这篇文章主要介绍了举例详解Python中threading模块的几个常用方法,threading模块用来创建和操作线程,是Python学习当中的重要知识,需要的朋友可以参考下
    2015-06-06
  • 使用Pycharm创建一个Django项目的超详细图文教程

    使用Pycharm创建一个Django项目的超详细图文教程

    Django是比较经典的Python web框架,最近刚好在项目中用到了Django,所以下面这篇文章主要给大家介绍了关于使用Pycharm创建一个Django项目的超详细图文教程,文中介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • python做http代理请求的项目实践

    python做http代理请求的项目实践

    本文主要介绍了使用Python Flask实现HTTP代理服务器的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-03-03
  • 从os.path到pathlib解析Python路径处理的高级应用

    从os.path到pathlib解析Python路径处理的高级应用

    在软件开发和系统管理领域,文件路径处理是一项基础却至关重要的任务,本文将深入探讨Python中路径处理的各个方面,从基础操作到高级技巧,大家可以根据需要进行选择
    2025-09-09
  • Python基于gevent实现文件字符串查找器

    Python基于gevent实现文件字符串查找器

    这篇文章主要介绍了Python基于gevent实现文件字符串查找器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Python创建字典的八种方式

    Python创建字典的八种方式

    今天小编就为大家分享一篇关于Python创建字典的八种方式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • python GUI库图形界面开发之PyQt5信号与槽机制、自定义信号基础介绍

    python GUI库图形界面开发之PyQt5信号与槽机制、自定义信号基础介绍

    这篇文章主要介绍了python GUI库图形界面开发之PyQt5信号与槽机制基础介绍,需要的朋友可以参考下
    2020-02-02
  • Python sns.distplot()方法的使用方法

    Python sns.distplot()方法的使用方法

    机器学习中经常会用到图形进行可视化,如在网格搜索(GridSearch)后对特征的重要性进行排序时,用到sns.barplot()函数按照重要程度输出特征,这篇文章主要给大家介绍了关于Python sns.distplot()方法的使用方法,需要的朋友可以参考下
    2022-03-03
  • Python中有哪些关键字及关键字的用法

    Python中有哪些关键字及关键字的用法

    这篇文章主要介绍了Python中有哪些关键字及关键字的用法,分享python中常用的关键字,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • Python批量修改文件名操作指南(文件名的替换、前缀、后缀的添加)

    Python批量修改文件名操作指南(文件名的替换、前缀、后缀的添加)

    很多时候我们手上有一堆文件,需要修改名称时需要一个一个修改,太麻烦了,这篇文章主要给大家介绍了关于Python批量修改文件名的相关资料,包括文件名的替换、前缀、后缀的添加的相关资料,需要的朋友可以参考下
    2024-04-04

最新评论