欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)

 更新時(shí)間:2018年04月18日 09:31:59   作者:1024python  
本文通過(guò)示例代碼給大家詳細(xì)介紹了Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

hasattr()函數(shù)

hasattr()函數(shù)用于判斷是否包含對(duì)應(yīng)的屬性

語(yǔ)法:

hasattr(object,name)

參數(shù):

object--對(duì)象

name--字符串,屬性名

返回值:

如果對(duì)象有該屬性返回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è)對(duì)象屬性值

語(yǔ)法:

getattr(object,name,default)

參數(shù):

object--對(duì)象

name--字符串,對(duì)象屬性

default--默認(rèn)返回值,如果不提供該參數(shù),在沒(méi)有對(duì)于屬性時(shí),將觸發(fā)AttributeError。

返回值:

返回對(duì)象屬性值

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)錯(cuò)
# 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è)置屬性值,該屬性必須存在

語(yǔ)法:

setattr(object,name,value)

參數(shù):

object--對(duì)象

name--字符串,對(duì)象屬性

value--屬性值

返回值:

無(wú)

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

語(yǔ)法:

setattr(object,name)

參數(shù):

object--對(duì)象

name--必須是對(duì)象的屬性

返回值:

無(wú)

示例:

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ù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Tesserocr庫(kù)的正確安裝方式

    Tesserocr庫(kù)的正確安裝方式

    今天小編就為大家分享一篇關(guān)于Tesserocr庫(kù)的正確安裝方式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • pycharm配置QtDesigner的超詳細(xì)方法

    pycharm配置QtDesigner的超詳細(xì)方法

    這篇文章主要介紹了pycharm配置QtDesigner的超詳細(xì)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決

    python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決

    這篇文章主要介紹了python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • python庫(kù)pydantic的入門(mén)簡(jiǎn)易教程

    python庫(kù)pydantic的入門(mén)簡(jiǎn)易教程

    本文主要介紹了python庫(kù)pydantic的入門(mén)簡(jiǎn)易教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 淺談Python 集合(set)類型的操作——并交差

    淺談Python 集合(set)類型的操作——并交差

    下面小編就為大家?guī)?lái)一篇淺談Python 集合(set)類型的操作——并交差。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • Python pywifi ERROR Open handle failed問(wèn)題及解決

    Python pywifi ERROR Open handle fai

    這篇文章主要介紹了Python pywifi ERROR Open handle failed問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python中創(chuàng)建二維數(shù)組

    Python中創(chuàng)建二維數(shù)組

    今天小編就為大家分享一篇關(guān)于Python中創(chuàng)建二維數(shù)組,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Python實(shí)現(xiàn)多屬性排序的方法

    Python實(shí)現(xiàn)多屬性排序的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)多屬性排序的方法,非常不錯(cuò),具有一定的參考價(jià)借鑒價(jià)值,需要的朋友可以參考下
    2018-12-12
  • Python實(shí)現(xiàn)操作Redis的高級(jí)用法分享

    Python實(shí)現(xiàn)操作Redis的高級(jí)用法分享

    redis-py是Python操作Redis的第三方庫(kù),它提供了與Redis服務(wù)器交互的API,本文為大家介紹了Python利用redis-py操作Redis的高級(jí)用法,需要的可以收藏一下
    2023-05-05
  • 深入理解Python中的*args和**kwargs參數(shù)(示例代碼)

    深入理解Python中的*args和**kwargs參數(shù)(示例代碼)

    *args和**kwargs是Python函數(shù)編程中極其有用的特性,它們?yōu)楹瘮?shù)參數(shù)的處理提供了極大的靈活性和強(qiáng)大的功能,這篇文章主要介紹了Python中的*args和**kwargs參數(shù),需要的朋友可以參考下
    2024-06-06

最新評(píng)論