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.)
如果一個(gè)對(duì)象里面有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.
獲取對(duì)象object的屬性或者方法慷妙。如果是返回的對(duì)象的屬性自脯,存在打印出來(lái)嫌套,不存在蛆挫,打印默認(rèn)值琉朽,默認(rèn)值可選淤袜。如果是返回的對(duì)象的方法痒谴,返回的是方法的內(nèi)存地址,可以在后面添加一對(duì)括號(hào)運(yùn)行這個(gè)方法铡羡。
>>> 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
.
給對(duì)象object的屬性賦值积蔚,若屬性不存在,先創(chuàng)建這個(gè)屬性再賦值
>>> 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
.
刪除指定對(duì)象的指定名稱(chēng)的屬性烦周,和setattr函數(shù)作用相反尽爆。
>>> 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
不能刪除對(duì)象的方法。
>>> test.hello()
Hello
>>> delattr(test, 'hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: hello
hello