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'>
当我们创建一个名为h的对象来表示Height类时,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:
我们创建了一个名为h的Height类的对象,并使用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()方法作为对象检查器。