淺析Python中的getattr(),setattr(),delattr(),hasattr()
getattr()函數(shù)是Python自省的核心函數(shù),具體使用大體如下:
獲取對象引用getattr
Getattr用于返回一個對象屬性,或者方法
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 對象中有屬性name則打印self.name的值,否則打印'not find' print getattr(Instance , 'age', 'not find') #如果Instance 對象中有屬性age則打印self.age的值,否則打印'not find' print getattr(a, 'method', 'default') #如果有方法method,否則打印其地址,否則打印default print getattr(a, 'method', 'default')() #如果有方法method,運行函數(shù)并打印None否則打印default
注:使用getattr可以輕松實現(xiàn)工廠模式。
例:一個模塊支持html、text、xml等格式的打印,根據(jù)傳入的formate參數(shù)的不同,調(diào)用不同的函數(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.
這是相對應的getattr()。參數(shù)是一個對象,一個字符串和一個任意值。字符串可能會列出一個現(xiàn)有的屬性或一個新的屬性。這個函數(shù)將值賦給屬性的。該對象允許它提供。例如,setattr(x,“foobar”,123)相當于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()相關的一組函數(shù)。參數(shù)是由一個對象(記住python中一切皆是對象)和一個字符串組成的。string參數(shù)必須是對象屬性名之一。該函數(shù)刪除該obj的一個由string指定的屬性。delattr(x, 'foobar')=del x.foobar
•hasattr用于確定一個對象是否具有某個屬性。
語法:
hasattr(object, name) -> bool
判斷object中是否有name屬性,返回一個布爾值。
>>> 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 Pygame實戰(zhàn)之飛機大戰(zhàn)的實現(xiàn)
飛機大戰(zhàn)想必是很多人童年時期的經(jīng)典游戲,這篇文章主要給大家介紹了關于如何利用python中的Pygame模塊寫一個簡單的飛機大戰(zhàn)小游戲的相關資料,需要的朋友可以參考下2022-03-03Appium+python自動化怎么查看程序所占端口號和IP
這篇文章主要介紹了Appium+python自動化怎么查看程序所占端口號和IP,本文以FQ工具 Lantern 為例,通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2019-06-06Python3爬蟲學習之MySQL數(shù)據(jù)庫存儲爬取的信息詳解
這篇文章主要介紹了Python3爬蟲學習之MySQL數(shù)據(jù)庫存儲爬取的信息,涉及Python3針對mysql數(shù)據(jù)庫的連接、信息存儲等相關操作技巧,需要的朋友可以參考下2018-12-12對pandas數(shù)據(jù)判斷是否為NaN值的方法詳解
今天小編就為大家分享一篇對pandas數(shù)據(jù)判斷是否為NaN值的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11python和pyqt實現(xiàn)360的CLable控件
這篇文章主要介紹了python和pyqt實現(xiàn)360的CLable控件示例,需要的朋友可以參考下2014-02-02Python中判斷input()輸入的數(shù)據(jù)的類型
在pyhton中,經(jīng)常會用到input()語句,但是input()語句輸入的內(nèi)容只能是字符串類型,而我們經(jīng)常要輸入int類型的數(shù)據(jù)等,這個時候就需要用到int()方法給輸入的內(nèi)容強制轉換,今天小編給大家介紹下Python中判斷input()輸入的數(shù)據(jù)的類型,感興趣的朋友跟隨小編一起看看吧2022-11-11