淺析Python中的getattr(),setattr(),delattr(),hasattr()
getattr()函數(shù)是Python自省的核心函數(shù),具體使用大體如下:
獲取對(duì)象引用getattr
Getattr用于返回一個(gè)對(duì)象屬性,或者方法
class A: def __init__(self): self.name = 'zhangjing' #self.age='' def method(self): print"method print" Instance = A() print getattr(Instance , 'name, 'not find') #如果Instance 對(duì)象中有屬性name則打印self.name的值,否則打印'not find' print getattr(Instance , 'age', 'not find') #如果Instance 對(duì)象中有屬性age則打印self.age的值,否則打印'not find' print getattr(a, 'method', 'default') #如果有方法method,否則打印其地址,否則打印default print getattr(a, 'method', 'default')() #如果有方法method,運(yùn)行函數(shù)并打印None否則打印default
注:使用getattr可以輕松實(shí)現(xiàn)工廠模式。
例:一個(gè)模塊支持html、text、xml等格式的打印,根據(jù)傳入的formate參數(shù)的不同,調(diào)用不同的函數(shù)實(shí)現(xiàn)幾種格式的輸出
import statsout def output(data, format="text"): output_function = getattr(statsout, "output_%s" % format) return output_function(data) setattr(object, name, value) This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.
這是相對(duì)應(yīng)的getattr()。參數(shù)是一個(gè)對(duì)象,一個(gè)字符串和一個(gè)任意值。字符串可能會(huì)列出一個(gè)現(xiàn)有的屬性或一個(gè)新的屬性。這個(gè)函數(shù)將值賦給屬性的。該對(duì)象允許它提供。例如,setattr(x,“foobar”,123)相當(dāng)于x.foobar = 123。
delattr(object, name)
This is a relative of setattr(). The arguments are
an object and a string. The string must be the name of one of the object's
attributes. The function deletes the named attribute, provided the object allows
it. For example, delattr(x, 'foobar') is
equivalent to del x.foobar.
與setattr()相關(guān)的一組函數(shù)。參數(shù)是由一個(gè)對(duì)象(記住python中一切皆是對(duì)象)和一個(gè)字符串組成的。string參數(shù)必須是對(duì)象屬性名之一。該函數(shù)刪除該obj的一個(gè)由string指定的屬性。delattr(x, 'foobar')=del x.foobar
•hasattr用于確定一個(gè)對(duì)象是否具有某個(gè)屬性。
語法:
hasattr(object, name) -> bool
判斷object中是否有name屬性,返回一個(gè)布爾值。
>>> li=["zhangjing","zhangwei"] >>> getattr(li,"pop") <built-in method pop of list object at 0x011DF6C0> >>> li.pop <built-in method pop of list object at 0x011DF6C0> >>> li.pop() 'zhangwei' >>> getattr(li,"pop")() 'zhangjing' >>>getattr(li, "append")("Moe")
- Python中g(shù)etattr函數(shù)和hasattr函數(shù)作用詳解
- 詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)
- 全面了解Python的getattr(),setattr(),delattr(),hasattr()
- 詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法
- 一文搞懂Python的hasattr()、getattr()、setattr()?函數(shù)用法
- 淺談python中的getattr函數(shù) hasattr函數(shù)
- python中hasattr()、getattr()、setattr()函數(shù)的使用
- 一次搞懂hasattr()/getattr()/setattr()在Python中的應(yīng)用
- Python學(xué)習(xí)筆記之getattr和hasattr用法示例詳解
相關(guān)文章
跟老齊學(xué)Python之再深點(diǎn),更懂list
對(duì)于list,由于她的確非常非常龐雜,在python中應(yīng)用非常廣泛,所以,雖然已經(jīng)介紹完畢了基礎(chǔ)內(nèi)容,這里還要用一講深入一點(diǎn)點(diǎn),往往越深入越...2014-09-09基于Python實(shí)現(xiàn)一個(gè)圖片壓縮工具
圖片壓縮是在保持圖像質(zhì)量的同時(shí)減小圖像文件大小的過程,本文將學(xué)習(xí)如何使用Python來實(shí)現(xiàn)一個(gè)簡(jiǎn)單但功能強(qiáng)大的圖片壓縮工具,以及如何在不同情境下進(jìn)行圖片壓縮,希望對(duì)大家有所幫助2024-01-01python 中的9個(gè)實(shí)用技巧,助你提高開發(fā)效率
這篇文章主要介紹了python 中的9個(gè)實(shí)用技巧,幫助大家提高python開發(fā)時(shí)的效率,感興趣的朋友可以了解下2020-08-08詳解在Python中使用Torchmoji將文本轉(zhuǎn)換為表情符號(hào)
這篇文章主要介紹了詳解在Python中使用Torchmoji將文本轉(zhuǎn)換為表情符號(hào),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Python實(shí)現(xiàn)圖形用戶界面和游戲開發(fā)的方法和技巧
GUI圖形用戶界面編程,我們可以通過python提供的豐富的組件,快速的實(shí)現(xiàn)使用圖形的界面和用戶交互, GUI編程類似于“搭積?”,將?個(gè)個(gè)組件(Widget)放到窗?中,這篇文章主要給大家介紹了基于Python的GUI圖形用戶界面編程的相關(guān)資料,需要的朋友可以參考下2023-05-05python?matplotlib繪畫十一種常見數(shù)據(jù)分析圖
這篇文章主要介紹了python?matplotlib繪畫十一種常見數(shù)據(jù)分析圖,文章主要繪制折線圖、散點(diǎn)圖、直方圖、餅圖等需要的小伙伴可以參考一下文章具體內(nèi)容2022-06-06Python中Thop庫(kù)的基本用例和參數(shù)說明
這篇文章主要給大家介紹了關(guān)于Python中Thop庫(kù)的基本用例和參數(shù)說明的相關(guān)資料,THOP是PyTorch非常實(shí)用的一個(gè)第三方庫(kù),可以統(tǒng)計(jì)模型的 FLOPs 和參數(shù)量,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05