Rumah > pembangunan bahagian belakang > Tutorial Python > Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码

Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码

不言
Lepaskan: 2018-04-18 11:10:41
asal
2097 orang telah melayarinya

本文通过示例代码给大家详细介绍了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}
Salin selepas log masuk


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
Salin selepas log masuk


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}
Salin selepas log masuk


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}
Salin selepas log masuk


补充示例:


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()
Salin selepas log masuk

相关推荐:

python3库numpy数组属性的查看方法


Atas ialah kandungan terperinci Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan