一文搞懂Python的hasattr()、getattr()、setattr()?函數(shù)用法
hasattr()
hasattr() 函數(shù)用來判斷某個類實例對象是否包含指定名稱的屬性或方法。
該函數(shù)的語法格式如下:
hasattr(obj, name)
其中 obj 指的是某個類的實例對象,name 表示指定的屬性名或方法名,返回BOOL值,有name特性返回True, 否則返回False。
例子:
class demo: def __init__ (self): self.name = "lily" def say(self): print("say hi") d = demo() print(hasattr(d, 'name')) print(hasattr(d, 'say')) print(hasattr(d, 'eat'))
運行結(jié)果如下:
True
True
False
getattr()
getattr() 函數(shù)獲取某個類實例對象中指定屬性的值。
該函數(shù)的語法格式如下:
getattr(object, name[, default])
其中,obj 表示指定的類實例對象,name 表示指定的屬性名,而 default 是可選參數(shù),用于設定該函數(shù)的默認返回值,即當函數(shù)查找失敗時,如果不指定 default 參數(shù),則程序?qū)⒅苯訄?AttributeError 錯誤,反之該函數(shù)將返回 default 指定的值。
例子:
class demo: def __init__ (self): self.name = "lily" def say(self): return "say hi" d = demo() print(getattr(d, 'name')) print(getattr(d, 'say')) print(getattr(d, 'eat'))
運行結(jié)果如下:
lily
<bound method demo.say of <__main__.demo object at 0x7f31c630d0a0>>
Traceback (most recent call last):
File "/test.py", line 11, in <module>
print(getattr(d, 'eat'))
AttributeError: 'demo' object has no attribute 'eat'
可以看到,對于類中已有的屬性,getattr() 會返回它們的值,而如果該名稱為方法名,則返回該方法的狀態(tài)信息;反之,如果該明白不為類對象所有,要么返回默認的參數(shù),要么程序報 AttributeError 錯誤。
需要注意的是,如果是返回的對象的方法,返回的是方法的內(nèi)存地址,如果需要運行這個方法,可以在后面添加一對括號。比如:
class demo: def __init__ (self): self.name = "lily" def say(self): return "say hi" def eat(self, something): return f"eat {something}" d = demo() print(getattr(d, 'name')) print(getattr(d, 'say')) print(getattr(d, 'eat')('apple')) print(getattr(d, 'eat', 'no eat')('banana'))
運行結(jié)果如下:
lily <bound method demo.say of <__main__.demo object at 0x7fe99b1ca0a0>> eat apple eat banana
setattr()
setattr() 函數(shù)最基礎的功能是修改類實例對象中的屬性值。其次,它還可以實現(xiàn)為實例對象動態(tài)添加屬性或者方法。
該函數(shù)的語法格式如下:
setattr(obj, name, value)
例子:
class demo: def __init__ (self): self.name = "lily" d = demo() print(getattr(d, 'name')) print('----------') setattr(d, 'name', 'tom') print(getattr(d, 'name')) print('----------') print(hasattr(d, 'age')) setattr(d, 'age', '18') print(hasattr(d, 'age')) print(getattr(d, 'age'))
運行結(jié)果如下:
lily
----------
tom
----------
False
True
18
到此這篇關于一文搞懂Python的hasattr()、getattr()、setattr() 函數(shù)用法的文章就介紹到這了,更多相關Python的hasattr()、getattr()、setattr() 函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 一次搞懂hasattr()/getattr()/setattr()在Python中的應用
- python中hasattr()、getattr()、setattr()函數(shù)的使用
- 詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法
- 詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)
- 全面了解Python的getattr(),setattr(),delattr(),hasattr()
- 淺談python中的getattr函數(shù) hasattr函數(shù)
- 淺析Python中的getattr(),setattr(),delattr(),hasattr()
- Python中getattr函數(shù)和hasattr函數(shù)作用詳解
- python中hasattr方法示例詳解
相關文章
python生成可執(zhí)行exe控制Microsip自動填寫號碼并撥打功能
這篇文章主要介紹了python生成可執(zhí)行exe控制Microsip自動填寫號碼并撥打,在這需要注意一個問題,必須是已經(jīng)運行Microsip.exe文件,具體實現(xiàn)代碼跟隨小編一起看看吧2021-06-06Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)用法實例分析
這篇文章主要介紹了Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)用法,結(jié)合實例形式分析了Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)相關功能、原理與使用技巧,需要的朋友可以參考下2019-12-12win10下python3.5.2和tensorflow安裝環(huán)境搭建教程
這篇文章主要為大家詳細介紹了win10下python3.5.2和tensorflow安裝環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09