描述:hasattr() 函数用于判断对象是否包含对应的属性。
语法:hasattr(object, name)
返回值:如果对象有该属性返回 True,否则返回 False。
实例:
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate()
print(hasattr(point1, 'x'))
print(hasattr(point1, 'y'))
print(hasattr(point1, 'z'))
print(hasattr(point1, 'no')) # 没有该属性
True
True
True
False
内置函数——hasattr() 函数
最新推荐文章于 2024-02-22 06:30:00 发布