如果對象具有給定的命名屬性,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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。