Python 中的内省2024 年 8 月 29 日 | 5 分钟阅读 在本教程中,我们将学习 Python 编程语言最强大的功能之一——自省。让我们简要介绍一下自省。 什么是自省?自省是一种在运行时确定对象类型的方法。我们知道,Python 中的一切都是对象,并且对各种自省方法有广泛的支持。自省是指检查内存中的其他模块和函数作为对象,获取有关它们的信息并进行操作的代码。它提供了熟悉对象属性和特性的便利。通过使用自省,我们可以动态地检查 Python 对象。 Python 是一种动态的、面向对象的、支持自省的语言,其影响力广泛而深入。自省功能使其成为比其他语言更强大的语言。 Python 提供了许多用于代码自省的函数和实用工具。我们还可以定义没有名称的调用函数和引用函数。 dir() 函数dir() 函数返回对象所属属性和方法的排序列表。让我们通过以下示例来理解,该示例使用 **dir()** 函数返回给定程序中所有可用方法的名称。 示例 - 输出 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] dir() 函数返回列表对象使用的所有方法和属性。 示例 - 2 输出 The methods and attributes are used with integer: ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes'] The methods and attributes are used with string: ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] Python type() 函数type() 函数返回对象的类型。让我们通过以下示例来理解。 示例 - 输出 <class 'int'> <class 'str'> 示例 - 2 输出 <class 'int'> <class 'str'> <class 'list'> <class 'dict'> <class 'tuple'> <class 'type'> <class 'function'> <class 'type'> <class '__main__.MyClass'> <class 'module'> Python hasattr() 函数hasattr() 函数检查对象是否具有某个属性。根据结果,如果对象具有给定属性,它将返回 True 或 False。让我们通过以下示例来理解。 示例 - 输出 True False Python id() 函数id() 函数返回对象的特殊 ID。让我们通过以下示例来理解。 示例 - 输出 The integer id is: 140736928548512 The string id is: 2423227163376 The list id is: 2423232862400 The set id is: 2423232820480 The list id is: 2423226957888 The object id is: 140736928328528 The functio id is: 2423231111520 The MyClass id is: 2423225796064 The obj id is: 2423232892640 The sys module id is: 2423227235840 Python sys 模块sys 模块允许我们与解释器使用或维护的系统特定变量和函数进行交互,并与解释器进行强交互的函数。让我们通过以下示例来理解。 示例 - 输出 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] win32 ['d:\\Python Project', 'D:\\python_project\\Myfirstdjangoproject\\Hello', 'c:\\users\\User\\appdata\\local\\programs\\python\\python38\\python38.zip', 'c:\\users\\User\\appdata\\local\\programs\\python\\python38\\DLLs', 'c:\\users\\User\\appdata\\local\\programs\\python\\python38\\lib', 'c:\\users\\User\\appdata\\local\\programs\\python\\python38', 'C:\\Users\\User\\.virtualenvs\\Django-ExvyqL3O', 'C:\\Users\\User\\.virtualenvs\\Django-ExvyqL3O\\lib\\site-packages'] 在上面的代码中,我们检查了 Python 版本、平台和搜索路径位置。 其他自省方法还有一些其他重要的自省方法,如下所示。
Python 中的自省属性我们将讨论一些提供对象有用信息的属性。让我们看一些重要的自省属性。
结论我们已经讨论了自省是什么以及它为什么重要。我们已经看到了一些重要的方法和示例。要了解更多关于自省的信息,您可以查看 Python inspect 模块。 下一主题Python 中的类装饰器 |
字符串是表示 Unicode 字符的字节。字符是长度为一的字符串。问题是 Python 不支持这种数据类型字符。示例:# 首先,我们将使用单引号创建一个字符串 String_1 = 'JavaTpoint is the best platform to learn Python' print("字符串...
阅读 2 分钟
获取输入是与用户交互或获取数据以提供某些结果的一种方式。Python 提供了两种内置方法来从键盘读取数据。这些方法如下。input(prompt) raw_input(prompt) input() input 函数用于所有最新版本的 Python。它接受...
阅读 2 分钟
在这个数字时代,实时信息变得至关重要,尤其是在规划旅行时。能够获取火车的实时运行状态和 PNR(乘客姓名记录)信息,无论是您经常旅行还是偶尔旅行,都能极大地改善您的旅行体验……
5 分钟阅读
数据分析、机器学习和Web开发只是强大的计算机语言Python众多用途中的几个。Python的关键特性之一是能够创建和访问包,包是可以跨项目重用的模块集合...
阅读 4 分钟
在本文中,我们将讨论 Python 中 os 路径 curdir 是什么,如何更改工作目录,以及如何使用 Python 检索当前目录。OS Python 模块提供了一种与操作系统通信的可移植方式。用于定位的工具...
阅读 3 分钟
在本Python教程中,我们将探讨如何解决错误、Python中的“syntaxerror return outside function”以及“can't assign to function call”。在Python中使用函数时,会发生函数外返回的错误。在编程方面,函数是非常...
阅读 6 分钟
有时,我们遇到一个问题,需要使用 Python 编程语言删除每个字符串的第一个字符。这个字符可能是偶然创建的,我们需要为单个字符串或整个列表执行此操作。这种...
阅读 4 分钟
对象检测 它是计算机视觉中分类和识别视频或图像中对象的任务。这种对象检测算法主要分为两种类型。它们是: 单阶段检测器 两阶段检测器 单阶段检测器 您只需在单阶段检测器中查看一次,该检测器使用完整的 CNN 来处理……
阅读 4 分钟
像 Python 这样的编程语言提供了不同的选项来开发 GUI,即图形用户界面。在所有 GUI 方法中,Tkinter 是最常用的。在接下来的教程中,我们将学习创建简单的复合利息 GUI 计算器的方法……
18 分钟阅读
Python的random包有一个内置函数shuffle()。它可以在Python中用来打乱一个序列(如列表或元组);打乱意味着改变集合中元素的索引。random.shuffle()的语法 我们使用shuffle()函数来改变索引...
5 分钟阅读
我们请求您订阅我们的新闻通讯以获取最新更新。
我们提供所有技术(如 Java 教程、Android、Java 框架)的教程和面试问题
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India