Python 有对象检查器吗?
在 Python 中,没有内置函数或普通函数可以充当对象检查器。但是,我们可以使用 `type()`、`help()`、`dir()`、`vars()` 等函数,或 `inspect` 模块来查找任何对象的属性、特性和方法。
此外,我们还有其他函数,如 `id()`、`getattr()`、`hasattr()`、`globals()`、`locals()`、`callable()`,这些函数有助于查看对象内部以了解其属性和方法。在这里,我们将使用一些内置函数来检查对象。在此之前,我们将创建一个简单的 Python 类及其对象,以便在本文中引用。
以下是定义 Python 类的语法
class Height:
"""
A height class that describes height as inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def tocentimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
使用 `type()` 函数作为对象检查器
在 Python 中,`type()` 是一个内置函数,用于获取有关对象的类型信息。
语法
type(object, bases, dict)
参数
object: 必需参数。如果只指定一个参数,则 `type()` 函数返回此对象的类型
返回值
这将返回一个新的类型类,或者本质上是一个元类。
示例
让我们使用上面的 Height 类来了解 `type()` 函数如何用作检查器。
class Height:
"""
A height class that describes height as inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def tocentimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
h = Height(2) # Create an object
print(type(h))
输出
<class '__main__.Height'>
当我们为 Height 类创建一个对象 h 时,`type()` 函数描述了对象 h 的类型,即 height。
使用 `help()` 函数作为对象检查器
`help()` 也是 Python 的内置函数,用于获取有关对象的帮助信息。以下是此函数的语法
help(object)
示例
再次以 Height 类为例,看看 `help()` 函数如何描述对象的详细信息。
class Height:
"""
A height class that describes height as inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def tocentimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
help(Height)
输出
Help on class Height in module __main__: class Height(builtins.object) | Height(inches) | | A height class that describes height as inches. | | Methods defined here: | | __init__(self, inches) | Assign the amount of inches to the height object | | tocentimeter(self) | Convert the inches to centimeters. | 1 inch = 2.54 centimeter | | ---------------------------------------------------------------------- | Data descriptors defined here:
当我们为 Height 类创建一个对象 h 时,`type()` 函数描述了对象 h 的类型,即 height。在这里,您可以看到与 Height() 类相关的所有内容。
使用 `dir()` 函数
`dir()` 是一个内置函数,它返回一个列表,其中包含对象的全部属性,它还将返回包含在 `__dict__` 属性中的键。以下是语法
dir(object)
参数
object: 调用给定对象的属性,这是一个可选参数。
示例
`dir()` 方法返回所有现有属性的列表。在列表的末尾,我们可以看到我们在类中实现的属性“tocentimeter”。此外,您还可以看到自动生成的属性,例如“__class__”、“__delattr__”、“__dict__”等。
class Height:
"""
A height class that describes height as inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def tocentimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
print(dir(Height))
输出
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'tocentimeter']
使用 `hasattr()` 函数
`hasattr()` 方法用于检查对象是否具有特定属性。以下是语法
hasattr(object, name )
参数
Object: 要检查的对象。
name: 它是一个字符串,是我们想要检查的特定属性的名称。
如果对象中存在所述属性,则返回“True”,否则返回“False”。
示例
让我们来看一个例子
class Height:
"""
A height class that describes height as inches.
"""
def __init__(self, inches):
"""
Assign the amount of inches to the height object
"""
self.inches = inches
def tocentimeter(self):
"""
Convert the inches to centimeters.
1 inch = 2.54 centimeter
"""
return self.inches * 2.54
h = Height(24)
print(hasattr(h,"tocentimeter"))
输出
True
同样,我们可以使用 `getattr()`、`id()` 和 `callable()` 方法作为对象检查器。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP