inspect模块

news/2025/2/11 19:21:05/文章来源:https://www.cnblogs.com/rolandhe/p/18710347

inspect.getmembers(object[, predicate]): 返回一个对象的所有成员,返回形式是一个列表,列表中每个元素是一个二元组,分别为成员名称及成员内容。
下面以一个函数为例,演示一下都有哪些成员。 注意,__builtins____globals__两个特殊属性里有非常多与函数本身无关的内容,占用行数太多,所以没有打印。

import inspectdef add(a: int=10, b:int=20, *, c=30):return a+b+cret = inspect.getmembers(add)
print(type(ret))
print(len(ret))print('-' * 25, '分隔线', '-' * 25)for item in ret:if item[0] in ('__builtins__', '__globals__'):continueprint(item)print('-' * 25, '分隔线', '-' * 25)
print(len(dir(add)))for item in dir(add):print(item)print('-' * 25, '分隔线', '-' * 25)
print(f'{vars(add) = }')print('-' * 25, '分隔线', '-' * 25)
print(f'{add.__annotations__ = }')
print(f'{add.__defaults__ = }')

输出结果:

<class 'list'>
37
------------------------- 分隔线 -------------------------
('__annotations__', {'a': <class 'int'>, 'b': <class 'int'>})
('__call__', <method-wrapper '__call__' of function object at 0x0000027C6F0904A0>)
('__class__', <class 'function'>)
('__closure__', None)
('__code__', <code object add at 0x0000027C6F0E9140, file "F:\RolandWork\PythonProjects\studyPython\forTest.py", line 3>)
('__defaults__', (10, 20))
('__delattr__', <method-wrapper '__delattr__' of function object at 0x0000027C6F0904A0>)
('__dict__', {})
('__dir__', <built-in method __dir__ of function object at 0x0000027C6F0904A0>)
('__doc__', None)
('__eq__', <method-wrapper '__eq__' of function object at 0x0000027C6F0904A0>)
('__format__', <built-in method __format__ of function object at 0x0000027C6F0904A0>)
('__ge__', <method-wrapper '__ge__' of function object at 0x0000027C6F0904A0>)
('__get__', <method-wrapper '__get__' of function object at 0x0000027C6F0904A0>)
('__getattribute__', <method-wrapper '__getattribute__' of function object at 0x0000027C6F0904A0>)
('__getstate__', <built-in method __getstate__ of function object at 0x0000027C6F0904A0>)
('__gt__', <method-wrapper '__gt__' of function object at 0x0000027C6F0904A0>)
('__hash__', <method-wrapper '__hash__' of function object at 0x0000027C6F0904A0>)
('__init__', <method-wrapper '__init__' of function object at 0x0000027C6F0904A0>)
('__init_subclass__', <built-in method __init_subclass__ of type object at 0x00007FF8BE5F4290>)
('__kwdefaults__', {'c': 30})
('__le__', <method-wrapper '__le__' of function object at 0x0000027C6F0904A0>)
('__lt__', <method-wrapper '__lt__' of function object at 0x0000027C6F0904A0>)
('__module__', '__main__')
('__name__', 'add')
('__ne__', <method-wrapper '__ne__' of function object at 0x0000027C6F0904A0>)
('__new__', <built-in method __new__ of type object at 0x00007FF8BE5F4290>)
('__qualname__', 'add')
('__reduce__', <built-in method __reduce__ of function object at 0x0000027C6F0904A0>)
('__reduce_ex__', <built-in method __reduce_ex__ of function object at 0x0000027C6F0904A0>)
('__repr__', <method-wrapper '__repr__' of function object at 0x0000027C6F0904A0>)
('__setattr__', <method-wrapper '__setattr__' of function object at 0x0000027C6F0904A0>)
('__sizeof__', <built-in method __sizeof__ of function object at 0x0000027C6F0904A0>)
('__str__', <method-wrapper '__str__' of function object at 0x0000027C6F0904A0>)
('__subclasshook__', <built-in method __subclasshook__ of type object at 0x00007FF8BE5F4290>)
------------------------- 分隔线 -------------------------
37
__annotations__
__builtins__
__call__
__class__
__closure__
__code__
__defaults__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__get__
__getattribute__
__getstate__
__globals__
__gt__
__hash__
__init__
__init_subclass__
__kwdefaults__
__le__
__lt__
__module__
__name__
__ne__
__new__
__qualname__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
------------------------- 分隔线 -------------------------
vars(add) = {}
------------------------- 分隔线 -------------------------
add.__defaults__ = (10, 20)
add.__annotations__ = {'a': <class 'int'>, 'b': <class 'int'>}

可以看出,函数返回的成员与使用dir函数返回的成员数量相同。

下面看一下一个类的成员

import inspectclass Foo:'''这是一个类'''prop1 = 123def m1(self):print('m1 method')
''''''
ret = inspect.getmembers(Foo)
print(type(ret))
print(len(ret))print('-' * 25, '分隔线', '-' * 25)for item in ret:print(item)print('-' * 25, '分隔线', '-' * 25)
print(len(dir(Foo)))for item in dir(Foo):print(item)print('-' * 25, '分隔线', '-' * 25)
print(f'{vars(Foo) = }')

输出结果:

<class 'list'>
29
------------------------- 分隔线 -------------------------
('__class__', <class 'type'>)
('__delattr__', <slot wrapper '__delattr__' of 'object' objects>)
('__dict__', mappingproxy({'__module__': '__main__', '__doc__': '这是一个类', 'prop1': 123, 'm1': <function Foo.m1 at 0x0000024513D139C0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>}))
('__dir__', <method '__dir__' of 'object' objects>)
('__doc__', '这是一个类')
('__eq__', <slot wrapper '__eq__' of 'object' objects>)
('__format__', <method '__format__' of 'object' objects>)
('__ge__', <slot wrapper '__ge__' of 'object' objects>)
('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>)
('__getstate__', <method '__getstate__' of 'object' objects>)
('__gt__', <slot wrapper '__gt__' of 'object' objects>)
('__hash__', <slot wrapper '__hash__' of 'object' objects>)
('__init__', <slot wrapper '__init__' of 'object' objects>)
('__init_subclass__', <built-in method __init_subclass__ of type object at 0x000002451395AB80>)
('__le__', <slot wrapper '__le__' of 'object' objects>)
('__lt__', <slot wrapper '__lt__' of 'object' objects>)
('__module__', '__main__')
('__ne__', <slot wrapper '__ne__' of 'object' objects>)
('__new__', <built-in method __new__ of type object at 0x00007FF8BE5F8DF0>)
('__reduce__', <method '__reduce__' of 'object' objects>)
('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>)
('__repr__', <slot wrapper '__repr__' of 'object' objects>)
('__setattr__', <slot wrapper '__setattr__' of 'object' objects>)
('__sizeof__', <method '__sizeof__' of 'object' objects>)
('__str__', <slot wrapper '__str__' of 'object' objects>)
('__subclasshook__', <built-in method __subclasshook__ of type object at 0x000002451395AB80>)
('__weakref__', <attribute '__weakref__' of 'Foo' objects>)
('m1', <function Foo.m1 at 0x0000024513D139C0>)
('prop1', 123)
------------------------- 分隔线 -------------------------
29
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__getstate__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
__weakref__
m1
prop1
------------------------- 分隔线 -------------------------
vars(Foo) = mappingproxy({'__module__': '__main__', '__doc__': '这是一个类', 'prop1': 123, 'm1': <function Foo.m1 at 0x0000024513D139C0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>})

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/882214.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

另辟新径实现 Blazor/MAUI 本机交互(三)

新建一个Maui blazor工程, 下面是工程关键文件解析. MainPage.xaml.cs 构造函数:通过 FindByName 方法查找名为 webView 的 WebView 控件,并将其赋值给 wvBrowser 变量。 创建 NativeBridge 的实例 api,并将 wvBrowser 传递给它。 使用 api.AddTarget 方法添加一个名为 dial…

nodejs如何处理Token?一文深入浅出JWT签名验签

关于token的那些事儿,一文深入浅出JWT签名验签前端开发中关于Token的那些事儿:深入浅出JWT签名验签 作为前端也要懂JWT,首先了解两个概念JWK JWT JWK JWK(RSA JSON Web Key)是一种用于表示 RSA 公钥或私钥的 JSON 对象,JWK 是 JSON Web Token (JWT) 和 JSON Web Encrypti…

前端开发中关于Token的那些事儿:深入浅出JWT签名验签

关于token的那些事儿,一文深入浅出JWT签名验签前端开发中关于Token的那些事儿:深入浅出JWT签名验签 作为前端也要懂JWT,首先了解两个概念JWK JWT JWK JWK(RSA JSON Web Key)是一种用于表示 RSA 公钥或私钥的 JSON 对象,JWK 是 JSON Web Token (JWT) 和 JSON Web Encrypti…

Eddystone 与 iBeacon

Eddystone 与 iBeacon 蓝牙信标 (Beacons) 是一种单向通讯方式,所以一般的用途就是发送提醒。 Beacons 是指使用蓝牙4.0(BLE)技术发射信号的小设备。 目前存活的 Beacons 标准有两个,分别是 Google 的 Eddystone 和 Apple 的 iBeacon. Eddystone Eddystone 是谷歌基于 Beac…

OrangePi 5 编译 Android12 源码

OrangePi 5 编译 Android12 源码 材料准备 源码下载地址 Orange Pi - Orangepi官方教程编译环境 在以下环境的 Ubuntu 虚拟机编译通过,基于 VMware Workstation 17 Pro.系统版本:ubuntu-18.04.6-lts-desktop-amd64; CPU:i5-8400,为 VM 分配 4 核; 内存:8G RAM + 16G swap…

Maui 基础 - Preferences 存储和检索应用程序的首选项

Maui 基础 Preferences 是 .NET MAUI 提供的一个静态类,用于存储和检索应用程序的首选项(即设置或配置)。它提供了一种简单的键值对存储机制,可以跨平台使用。每个平台使用其本地的存储机制来实现这些功能,例如:iOS 使用 NSUserDefaults Android 使用 SharedPreferences …

另辟新径实现 Blazor/MAUI 本机交互(一)

本系列由浅入深逐个文件解析工作原理 目录:WebViewNativeApi.cs NativeApi.cs MainPage.xaml.cs 实战 串口 小票机 蓝牙WebViewNativeApi.cs WebViewNativeApi.cs 文件中的代码实现了一个 NativeBridge 类,用于在 .NET MAUI 应用程序中的 WebView 和本地代码之间进行通信。以下…

AI 如何重塑劳动力市场:基于 Claude 数据的深度分析

前言 本文翻译自 Anthropic 今天发布的 The Anthropic Economic Index ,经济指数报告,这份报告基于 Claude 的数据对目前的 AI 使用情况做了汇总。 引言 在未来的几年里,人工智能系统将对人们的工作方式产生重大影响。因此,我们推出了 Anthropic Economic Index,这是一个旨…

Nacos Python SDK 强势来袭,动态管理大模型 Prompt!

Nacos 从 0.8.0 版本开始就一直参与 Python 生态建设,努力作为 Python 生态中分布式微服务发现和配置管理的解决方案一直往前演进。目前随着 AI 领域的发展,Nacos 社区的 Python 开发者用户越来越多,因此这次我们迭代了 Python 的 GA 稳定版本,对不少历史问题做了修复以及易…

踩坑记录-二分搜索的不同情况

二分搜索的不同情况 二分搜索可以用来查找满足条件的值,但是满足条件的值可能只有1个,也可能有多个。比如查找1的索引,对于【1,1,2,2】来说,就有2个。一般要求的就是:满足条件最大值/满足条件最小值。 二分搜索详细介绍可以参考:https://programmercarl.com/0704.二分…

《ESP32-S3使用指南—IDF版 V1.6》第五章 搭建开发环境

第五章 搭建开发环境 1)实验平台:正点原子DNESP32S3开发板 2)章节摘自【正点原子】ESP32-S3使用指南—IDF版 V1.6 3)购买链接:https://detail.tmall.com/item.htm?&id=768499342659 4)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/esp32/AT…

内测之家介绍

内测之家:助力应用开发与迭代的专业平台内测之家是一款功能强大且全面的应用内测与管理平台,专为 iOS 和 Android 开发者打造,旨在为他们提供便捷高效、安全可靠的一站式服务。无论是从资源安全到传输安全,还是从数据保护到应用管理、统计分析,内测之家都展现出卓越的能力…