python 内置函数(三): hasattr(), setat
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