网站首页 > 资源文章 正文
inspect模块提供了一系列函数用于自省操作。下面进行叙述。
涉及到的类和函数如下:
def test_func(name, age, sex="男", *ids, **info):
"""
返回个人信息
:param name: 姓名
:param age: 年龄
:param sex: 性别
:return: 个人信息
"""
return name + "," + age + "," + sex
?
class Person(object):
"""
这是一个测试类
"""
?
def __init__(self, name, age):
self.name = name
self.age = age
?
def say(self):
print(f"{self.name}在谈话")
?
?
p = Person("小明", 23)
复制代码
1 检查对象类型
1.1 is系列方法
方法 | 说明 |
ismodule | 检查对象是否为模块 |
isclass | 检查对象是否为类 |
isfunction | 检查对象是否为函数 |
ismethod | 检查对象是否为方法 |
isbuiltin | 检查对象是否为内建函数或方法 |
eg:
import inspect
from test_study import driver_demo
?
print(inspect.ismodule(driver_demo))
复制代码
result:
True
复制代码
print(inspect.isfunction(test_func))
复制代码
result:
True
复制代码
print(inspect.isclass(Person))
print(inspect.ismethod(p.say))
复制代码
result:
True
True
复制代码
print(inspect.isbuiltin(print))
复制代码
result:
True
复制代码
1.2 isroutine方法
检查对象是否为函数、方法、内建函数或方法等可调用类型,和is系列的作用完全一致
if inspect.isroutine(p.say):
p.say()
复制代码
result:
小明在谈话
复制代码
1.3 判断对象是否可调用
如果只是判断对象是否可调用,可以采取更简单的判断办法
from collections.abc import Callable
print(isinstance(p.say, Callable))
复制代码
result:
True
复制代码
2 获取对象信息
2.1 getmembers方法
该方法是dir()方法的扩展版
print(inspect.getmembers(p.say))
复制代码
result:
[('__call__', <method-wrapper '__call__' of method object at 0x0000020F307850C8>), ('__class__', <class 'method'>), ('__delattr__', <method-wrapper '__delattr__' of method object at 0x0000020F307850C8>), ('__dir__', <built-in method __dir__ of method object at 0x0000020F307850C8>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of method object at 0x0000020F307850C8>), ('__format__', <built-in method __format__ of method object at 0x0000020F307850C8>), ('__func__', <function Person.say at 0x0000020F30E8C048>), ('__ge__', <method-wrapper '__ge__' of method object at 0x0000020F307850C8>), ('__get__', <method-wrapper '__get__' of method object at 0x0000020F307850C8>), ('__getattribute__', <method-wrapper '__getattribute__' of method object at 0x0000020F307850C8>), ('__gt__', <method-wrapper '__gt__' of method object at 0x0000020F307850C8>), ('__hash__', <method-wrapper '__hash__' of method object at 0x0000020F307850C8>), ('__init__', <method-wrapper '__init__' of method object at 0x0000020F307850C8>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x00007FFE1190E030>), ('__le__', <method-wrapper '__le__' of method object at 0x0000020F307850C8>), ('__lt__', <method-wrapper '__lt__' of method object at 0x0000020F307850C8>), ('__ne__', <method-wrapper '__ne__' of method object at 0x0000020F307850C8>), ('__new__', <built-in method __new__ of type object at 0x00007FFE1190E030>), ('__reduce__', <built-in method __reduce__ of method object at 0x0000020F307850C8>), ('__reduce_ex__', <built-in method __reduce_ex__ of method object at 0x0000020F307850C8>), ('__repr__', <method-wrapper '__repr__' of method object at 0x0000020F307850C8>), ('__self__', <__main__.Person object at 0x0000020F30E89748>), ('__setattr__', <method-wrapper '__setattr__' of method object at 0x0000020F307850C8>), ('__sizeof__', <built-in method __sizeof__ of method object at 0x0000020F307850C8>), ('__str__', <method-wrapper '__str__' of method object at 0x0000020F307850C8>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x00007FFE1190E030>)]
复制代码
2.2 getmodule方法
类所在模块的模块名,和module方法不同,这里所返回的模块名是一个对象,可以直接调用,而不是字符串形式
print(inspect.getmodule(p.say))
复制代码
result:
<module '__main__' from 'D:/workspace/test_study/test_study/test.py'>
复制代码
2.3 getfile方法
获取对象所定义的文件名
print(inspect.getfile(p.say))
复制代码
result:
D:/workspace/test_study/test_study/test.py
复制代码
2.4 getsource方法
获取对象的源代码
print(inspect.getsource(p.say))
复制代码
result:
def say(self):
print(f"{self.name}在谈话")
复制代码
2.5 getfullargspec方法
获取方法定义时所声明的参数,其结果是一个元组,分别是(普通参数名列表,*参数名,**参数名,默认值元组)
print(inspect.getfullargspec(test_func))
复制代码
result:
FullArgSpec(args=['name', 'age', 'sex'], varargs='ids', varkw='info', defaults=('男',), kwonlyargs=[], kwonlydefaults=None, annotations={})
复制代码
2.6 getargvalues方法
仅用于栈桢,获取栈桢中函数当前调用时的参数值,其结果是一个元组,分别是(普通参数名列表,*参数名,**参数名,栈的locals())
def test_func(name, age, sex="男", *ids, **info, ):
"""
返回个人信息
:param name: 姓名
:param age: 年龄
:param sex: 性别
:return: 个人信息
"""
print(inspect.getargvalues(inspect.currentframe()))
return name + "," + age + "," + sex
print(test_func("小明", '23'))
复制代码
result:
ArgInfo(args=['name', 'age', 'sex'], varargs='ids', keywords='info', locals={'name': '小明', 'age': '23', 'sex': '男', 'ids': (), 'info': {}})
小明,23,男
猜你喜欢
- 2024-12-09 蔡司复合式三坐标O-INSPECT校准Dotscan时报错怎么办
- 2024-12-09 建立Motion Studio专案
- 2024-12-09 康耐视visionpro9.8-BeadInspect工具详细使用流程
- 2024-12-09 DeepInspect:深度神经网络的黑盒木马检测与缓解框架
- 2024-12-09 GOM Inspect 免费3D量测与检测软件简介
- 2024-12-09 喜讯|温泽exaCT U荣获2018年inspect大奖
- 2024-12-09 PowerInspect2016——非常好用的3D检测类软件
- 2024-12-09 python自动化工具:pywinauto
- 2024-12-09 MAS控制器教程连接視覺軟件Inspect Express
- 2024-12-09 Python常用标准库及第三方库6-inspect模块(检查对象)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)