如何使用getattr函数在Python中获取指定对象的属性值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计494个文字,预计阅读时间需要2分钟。
Python 文档中关于 `getattr` 函数的描述如下:
获取对象名为 `name` 的属性值。`name` 必须是一个字符串。如果该字符串是对象属性之一的名字,则结果为该属性的值。例如:
英文文档:
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.
获取对象的属性值
说明:
1. 函数功能是从对象object中获取名称为name的属性,等效与调用object.name。
本文共计494个文字,预计阅读时间需要2分钟。
Python 文档中关于 `getattr` 函数的描述如下:
获取对象名为 `name` 的属性值。`name` 必须是一个字符串。如果该字符串是对象属性之一的名字,则结果为该属性的值。例如:
英文文档:
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.
获取对象的属性值
说明:
1. 函数功能是从对象object中获取名称为name的属性,等效与调用object.name。

