小白教程
所有教程
关于
Search
172.70.179.116
172.70.179.116
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python func getattr”的源代码
本页内容
上一节:
Python_func_classmethod
下一节:
Python_func_locals
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:getattr()}}[[Category:python3 built in functions|51]] = Python getattr() 函数 = [[python built in functions|Python 内置函数]] == 描述 == '''getattr()''' 函数用于返回一个对象属性值。 == 语法 == getattr 语法: <sample title="" desc="" lang="python" hererun="1"> getattr(object, name[, default]) </sample> == 参数 == * object -- 对象。 * name -- 字符串,对象属性。 * default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。 == 返回值 == 返回对象属性值。 以下实例展示了 getattr 的使用方法: <sample title="" desc="" lang="python" hererun="1"> >>>class A(object): ... bar = 1 ... >>> a = A() >>> getattr(a, 'bar') # 获取属性 bar 值 1 >>> getattr(a, 'bar2') # 属性 bar2 不存在,触发异常 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'A' object has no attribute 'bar2' >>> getattr(a, 'bar2', 3) # 属性 bar2 不存在,但设置了默认值 3 >>> </sample> [[python built in functions|Python 内置函数]]
返回至“
Python func getattr
”。
上一节:
Python_func_classmethod
下一节:
Python_func_locals