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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。