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()函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。