您的位置:首页 > 移动开发 > Objective-C

Python 内建函数 - getattr(object, name[, default])

2017-03-15 17:34 435 查看
Manual

直译

实例

拓展阅读

Manual

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中名为name的属性值。name必须是字符串,如果字符串是对象某个属性名,结果为该属性值,例如getattr(x, ‘foobar’)等价于x.foobar。如果给定的name不存在,会根据提供的默认值进行返回,否则会引发AttributeError。

实例

>>> class CSDN:
def foobar(self):
print('Hello CSDNer!')
>>> getattr(CSDN, 'foobar')
<function CSDN.foobar at 0x03944F18>
>>> CSDN.foobar
<function CSDN.foobar at 0x03944F18>


拓展阅读

delattr()

hasattr()

setattr()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python