詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)
hasattr()函數(shù)
hasattr()函數(shù)用于判斷是否包含對應(yīng)的屬性
語法:
hasattr(object,name)
參數(shù):
object--對象
name--字符串,屬性名
返回值:
如果對象有該屬性返回True,否則返回False
示例:
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) obj=People('aaa') print(hasattr(People,'country')) #返回值:True print('country' in People.__dict__) #返回值:True print(hasattr(obj,'people_info')) #返回值:True print(People.__dict__) ##{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10205d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
getattr()函數(shù)
描述:
getattr()函數(shù)用于返回一個(gè)對象屬性值
語法:
getattr(object,name,default)
參數(shù):
object--對象
name--字符串,對象屬性
default--默認(rèn)返回值,如果不提供該參數(shù),在沒有對于屬性時(shí),將觸發(fā)AttributeError。
返回值:
返回對象屬性值
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) obj=getattr(People,'country') print(obj) #返回值China #obj=getattr(People,'countryaaaaaa') #print(obj) #報(bào)錯 # File "/getattr()函數(shù).py", line 32, in <module> # obj=getattr(People,'countryaaaaaa') # AttributeError: type object 'People' has no attribute 'countryaaaaaa' obj=getattr(People,'countryaaaaaa',None) print(obj) #返回值None
setattr()函數(shù)
描述:
setattr函數(shù),用于設(shè)置屬性值,該屬性必須存在
語法:
setattr(object,name,value)
參數(shù):
object--對象
name--字符串,對象屬性
value--屬性值
返回值:
無
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) obj=People('aaa') setattr(People,'x',111) #等同于People.x=111 print(People.x) #obj.age=18 setattr(obj,'age',18) print(obj.__dict__) #{'name': 'aaa', 'age': 18} print(People.__dict__) #{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1007d5620>, 'people_info': <function People.people_info at 0x10215d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None, 'x': 111}
delattr()函數(shù)
描述:
delattr函數(shù)用于刪除屬性
delattr(x,'foobar)相當(dāng)于del x.foobar
語法:
setattr(object,name)
參數(shù):
object--對象
name--必須是對象的屬性
返回值:
無
示例:
class People: country='China' def __init__(self,name): self.name=name def people_info(self): print('%s is xxx' %(self.name)) delattr(People,'country') #等同于del People.country print(People.__dict__) {'__module__': '__main__', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10073d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
補(bǔ)充示例:
class Foo: def run(self): while True: cmd=input('cmd>>: ').strip() if hasattr(self,cmd): func=getattr(self,cmd) func() def download(self): print('download....') def upload(self): print('upload...') # obj=Foo() # obj.run()
總結(jié)
以上所述是小編給大家介紹的詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 一次搞懂hasattr()/getattr()/setattr()在Python中的應(yīng)用
- 一文搞懂Python的hasattr()、getattr()、setattr()?函數(shù)用法
- python中hasattr()、getattr()、setattr()函數(shù)的使用
- 詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法
- 全面了解Python的getattr(),setattr(),delattr(),hasattr()
- 淺談python中的getattr函數(shù) hasattr函數(shù)
- 淺析Python中的getattr(),setattr(),delattr(),hasattr()
- Python中g(shù)etattr函數(shù)和hasattr函數(shù)作用詳解
- python中hasattr方法示例詳解
相關(guān)文章
tensorflow: variable的值與variable.read_value()的值區(qū)別詳解
今天小編就為大家分享一篇tensorflow: variable的值與variable.read_value()的值區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Pycharm 如何設(shè)置HTML文件自動補(bǔ)全代碼或標(biāo)簽
這篇文章主要介紹了Pycharm 如何設(shè)置HTML文件自動補(bǔ)全代碼或標(biāo)簽,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05Python實(shí)戰(zhàn)小游戲飛機(jī)大戰(zhàn)詳解
飛機(jī)大戰(zhàn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂的場景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫一個(gè)簡單的飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下2021-11-11