Python/ref func hasattr
来自菜鸟教程
<languages />
Python hasattr()函数
例
检查“人”对象是否具有“年龄”属性:
class Person:
name = "John"
age = 36
country = "Norway"
x = hasattr(Person, 'age')
定义和用法
The
hasattr()
函数返回
True
如果指定的对象具有指定的属性,否则
False
.
句法
hasattr(object, attribute)
参数值
| 参数 | 描述 |
|---|---|
| object | 需要。一个东西。 |
| attribute | 您要检查的属性名称是否存在 |
相关页面
The delattr()
函数,删除属性
The getattr()
函数,获取属性值
The setattr()
函数,用于设置属性的值