zoukankan      html  css  js  c++  java
  • Python getattr() 函数

    描述

    getattr() 函数用于返回一个对象属性值。

    语法

    getattr 语法:

    getattr(object, name[, default])
    

    参数

    • object -- 对象。
    • name -- 字符串,对象属性。
    • default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。

    返回值

    返回对象属性值。

    实例

    以下实例展示了 getattr 的使用方法:

    >>>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

    >>>

    >>>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 >>>
  • 相关阅读:
    代理模式
    组合模式
    yum配置文件详解
    责任链模式
    git看不到别人创建的远程分支
    学习gulpfile.babel.js随笔
    遍历数组的方法
    解决Error: ENOENT: no such file or directory, scandir 安装node-sass报错
    window对象
    Moment.js的一些用法
  • 原文地址:https://www.cnblogs.com/ggzhangxiaochao/p/9041373.html
Copyright © 2011-2022 走看看