python 内置函数(三): hasattr(), setattr(), getattr(), delattr()

hasattr(object, name)

The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

如果一个对象里面有name属性或者name方法,返回True,否则返回False

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'

>>> test = Test('tom')
>>> hasattr(test, 'name')  # object has 'name' attribute
True
>>> hasattr(test, 'hello')  # object has 'hello' function
True

getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

获取对象object的属性或者方法。如果是返回的对象的属性,存在打印出来,不存在,打印默认值,默认值可选。如果是返回的对象的方法,返回的是方法的内存地址,可以在后面添加一对括号运行这个方法。

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'
>>> test = Test('tom')
>>> getattr(test, 'name')
'tom'

>>> getattr(test, 'hello')
<bound method Test.hello of <__main__.Test object at 0x7fd09647e110>>

>>> getattr(test, 'hello')()
Hello

>>> getattr(test, 'noexist')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute 'noexist'
'Test' object has no attribute 'noexist'

>>> getattr(test, 'noexist', 'Yes')
'Yes'

setattr(object, name, value)

This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.

给对象object的属性赋值,若属性不存在,先创建这个属性再赋值

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'
>>> test = Test('tom')

>>> hasattr(test, 'age')
False
>>> setattr(test, 'age', 20)
>>> test.age
20
>>> hasattr(test, 'age')
True

delattr(object, name)

This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar.

删除指定对象的指定名称的属性,和setattr函数作用相反。

>>> class Test(object):
...     def __init__(self, name):
...         self.name = name
...     def hello(self):
...         print 'Hello'
>>> test = Test('tom')
>>> hasattr(test, 'name')
True

>>> test.name
'tom'

>>> delattr(test, 'name')

>>> hasattr(test, 'name')
False

>>> test.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute 'name'
'Test' object has no attribute 'name'

>>> delattr(test, 'name')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: name
name

不能删除对象的方法。

>>> test.hello()
Hello

>>> delattr(test, 'hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: hello
hello
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 函数调用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜阅读 1,209评论 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • 一个人生活,觉得日子都变长了――小津安二郎。 做事不用急急忙忙,想到什么要做的事,都会慢慢做。生怕等会没...
    木子妍心阅读 256评论 0 0
  • 16/07/20 JS与OC的交互 iOS7之前 通过拦截URL的方式实现交互 在iOS7之前,实现js与oc的交...
    朱益达阅读 378评论 0 0
  • 这是一篇不太长的文章,阅读大约需要3分钟 不想做将军的士兵不是好士兵 ——这是在团建回来的路上,和部门老大简短交流...
    RRRocker阅读 625评论 0 0