首頁 > 後端開發 > Python教學 > Python3 中hasattr()、getattr()、setattr()、delattr()函數及範例程式碼

Python3 中hasattr()、getattr()、setattr()、delattr()函數及範例程式碼

不言
發布: 2018-04-18 11:10:41
原創
2098 人瀏覽過

本文透過範例程式碼給大家詳細介紹了Python3 中hasattr()、getattr()、setattr()、delattr()函數,非常不錯,具有參考借鑒價值,需要的朋友參考下吧

hasattr()函數

hasattr()函數用來判斷是否包含對應的屬性

語法:

hasattr(object,name)

參數:

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__)
##{&#39;__module__&#39;: &#39;__main__&#39;, &#39;country&#39;: &#39;China&#39;, &#39;__init__&#39;: <function People.__init__ at 0x1006d5620>, &#39;people_info&#39;: <function People.people_info at 0x10205d1e0>, &#39;__dict__&#39;: <attribute &#39;__dict__&#39; of &#39;People&#39; objects>, &#39;__weakref__&#39;: <attribute &#39;__weakref__&#39; of &#39;People&#39; objects>, &#39;__doc__&#39;: None}
登入後複製


##getattr()函數

描述:

getattr()函數用來傳回一個物件屬性值

語法:

getattr(object,name,default)

#參數:

object--物件

##name--字串,物件屬性

default--預設回傳值,如果不提供該參數,在沒有對於屬性時,將觸發AttributeError。

傳回值:

傳回物件屬性值

#

class People:
  country=&#39;China&#39;
  def __init__(self,name):
    self.name=name

  def people_info(self):
    print(&#39;%s is xxx&#39; %(self.name))
obj=getattr(People,&#39;country&#39;)
print(obj)
#返回值China
#obj=getattr(People,&#39;countryaaaaaa&#39;)
#print(obj)
#报错
# File "/getattr()函数.py", line 32, in <module>
#   obj=getattr(People,&#39;countryaaaaaa&#39;)
# AttributeError: type object &#39;People&#39; has no attribute &#39;countryaaaaaa&#39;
obj=getattr(People,&#39;countryaaaaaa&#39;,None)
print(obj)
#返回值None
登入後複製


setattr()函數描述:

setattr函數,用於設定屬性值,該屬性必須存在

語法:

setattr(object,name,value)

參數:

object--物件

##name--字串,物件屬性

value--屬性值

傳回值:

#
class People:
  country=&#39;China&#39;
  def __init__(self,name):
    self.name=name
  def people_info(self):
    print(&#39;%s is xxx&#39; %(self.name))
obj=People(&#39;aaa&#39;)
setattr(People,&#39;x&#39;,111) #等同于People.x=111
print(People.x)
#obj.age=18
setattr(obj,&#39;age&#39;,18)
print(obj.__dict__)
#{&#39;name&#39;: &#39;aaa&#39;, &#39;age&#39;: 18}
print(People.__dict__)
#{&#39;__module__&#39;: &#39;__main__&#39;, &#39;country&#39;: &#39;China&#39;, &#39;__init__&#39;: <function People.__init__ at 0x1007d5620>, &#39;people_info&#39;: <function People.people_info at 0x10215d1e0>, &#39;__dict__&#39;: <attribute &#39;__dict__&#39; of &#39;People&#39; objects>, &#39;__weakref__&#39;: <attribute &#39;__weakref__&#39; of &#39;People&#39; objects>, &#39;__doc__&#39;: None, &#39;x&#39;: 111}
登入後複製



delattr()函數

描述:

delattr函數用來刪除屬性

delattr(x,'foobar)相當於del x.foobar

語法:

setattr(object,name)

參數:

object--物件

name--必須是物件的屬性

傳回值:

#範例:

class People:
  country=&#39;China&#39;
  def __init__(self,name):
    self.name=name
  def people_info(self):
    print(&#39;%s is xxx&#39; %(self.name))
delattr(People,&#39;country&#39;) #等同于del People.country
print(People.__dict__)
{&#39;__module__&#39;: &#39;__main__&#39;, &#39;__init__&#39;: <function People.__init__ at 0x1006d5620>, &#39;people_info&#39;: <function People.people_info at 0x10073d1e0>, &#39;__dict__&#39;: <attribute &#39;__dict__&#39; of &#39;People&#39; objects>, &#39;__weakref__&#39;: <attribute &#39;__weakref__&#39; of &#39;People&#39; objects>, &#39;__doc__&#39;: None}
登入後複製


補充範例:

class Foo:
  def run(self):
    while True:
      cmd=input(&#39;cmd>>: &#39;).strip()
      if hasattr(self,cmd):
        func=getattr(self,cmd)
        func()
  def download(self):
    print(&#39;download....&#39;)
  def upload(self):
    print(&#39;upload...&#39;)
# obj=Foo()
# obj.run()
登入後複製


相關推薦:

python3函式庫numpy陣列屬性的檢視方法

#

以上是Python3 中hasattr()、getattr()、setattr()、delattr()函數及範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板