如果对象具有给定的命名属性,hasattr() 方法返回 true,否则返回 false。
用法:
hasattr(object, name)
hasattr()
由getattr() 调用以检查是否要提高AttributeError
。
参数:
hasattr()
方法有两个参数:
- object- 要检查其命名属性的对象
- name- 要搜索的属性的名称
返回:
hasattr()
方法返回:
- True, 如果对象具有给定的命名属性
- False, 如果对象没有给定的命名属性
示例:hasattr() 如何在 Python 中工作?
class Person:
age = 23
name = 'Adam'
person = Person()
print('Person has age?:', hasattr(person, 'age'))
print('Person has salary?:', hasattr(person, 'salary'))
输出
Person has age?: True Person has salary?: False
相关用法
- Python hasattr()用法及代码示例
- Python hashlib.sha3_256()用法及代码示例
- Python hashlib.sha3_512()用法及代码示例
- Python hashlib.blake2s()用法及代码示例
- Python hashlib.blake2b()用法及代码示例
- Python hash()用法及代码示例
- Python hashlib.sha3_224()用法及代码示例
- Python hashlib.shake_256()用法及代码示例
- Python hashlib.shake_128()用法及代码示例
- Python hashlib.sha3_384()用法及代码示例
- Python statistics harmonic_mean()用法及代码示例
- Python OpenCV haveImageReader()用法及代码示例
- Python help()用法及代码示例
- Python html.escape()用法及代码示例
- Python html.unescape()用法及代码示例
- Python math hypot()用法及代码示例
- Python hex()用法及代码示例
- Python torch.distributed.rpc.rpc_async用法及代码示例
- Python torch.nn.InstanceNorm3d用法及代码示例
- Python pandas.arrays.IntervalArray.is_empty用法及代码示例
注:本文由纯净天空筛选整理自 Python hasattr()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。