1. 程式人生 > >(轉)詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數及示例代碼數

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

div 是否 ror try default count kref obj www

原文:https://www.jb51.net/article/138363.htm

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__)
##{‘__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()函數

描述:

getattr()函數用於返回一個對象屬性值

語法:

getattr(object,name,default)

參數:

object--對象

name--字符串,對象屬性

default--默認返回值,如果不提供該參數,在沒有對於屬性時,將觸發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)
#報錯
# File "/getattr()函數.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()函數

描述:

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

語法:

setattr(object,name,value)

參數:

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()函數

描述:

delattr函數用於刪除屬性

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

語法:

setattr(object,name)

參數:

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}

補充示例:

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()

總結

以上所述是小編給大家介紹的詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

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