Python 的 hasattr(~)
方法返回 Boolean
指示提供的对象是否具有指定的属性。
参数
1. object
| object
我们正在检查属性的对象。
2. name
| string
表示我们正在检查的属性名称的字符串。
例子
检查对象 my_cat
是否具有属性 age
:
class Cat():
# This is constructor for python
def __init__(self, name, age):
self.name = name
self.age = age
# Teaching each cat how to meow
def meow(self):
print("I am a " + str(self.age) + " year old " + self.name)
# Creating an instance my_cat representing my cat Roxas
my_cat = Cat("Roxas", 26)
hasattr(my_cat, "age")
True
检查对象 my_cat
是否具有属性 height
:
hasattr(my_cat, "height")
False
相关用法
- Python hasattr()用法及代码示例
- Python hashlib.sha3_256()用法及代码示例
- Python BeautifulSoup has_attr方法用法及代码示例
- Python hashlib.sha3_512()用法及代码示例
- Python hashlib.shake_256()用法及代码示例
- Python hashlib.shake_128()用法及代码示例
- Python hashlib.blake2s()用法及代码示例
- Python hashlib.blake2b()用法及代码示例
- Python hash()用法及代码示例
- Python hashlib.sha3_224()用法及代码示例
- Python hashlib.pbkdf2_hmac用法及代码示例
- Python hashlib.sha3_384()用法及代码示例
- Python statistics harmonic_mean()用法及代码示例
- Python OpenCV haveImageReader()用法及代码示例
- Python help()用法及代码示例
- Python NumPy hstack方法用法及代码示例
- Python http.HTTPStatus用法及代码示例
- Python help方法用法及代码示例
- Python NumPy hypot方法用法及代码示例
- Python html.escape()用法及代码示例
- Python html.unescape()用法及代码示例
- Python math hypot()用法及代码示例
- Python hex方法用法及代码示例
- Python hex()用法及代码示例
- Python NumPy hsplit方法用法及代码示例
注:本文由纯净天空筛选整理自Arthur Yanagisawa大神的英文原创作品 Python | hasattr method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。