zoukankan      html  css  js  c++  java
  • [Python学习笔记-001] 内置函数getattr()

    如果某个实例foo有多个方法, 当对foo的每一个方法我们都需要使用try ... except ...进行包装的时候,内置函数getattr()可以用来精简代码。

    1. getattr()的用法

    # https://docs.python.org/2/library/functions.html#getattr
    
    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.

    简单来说,getattr()就是获取对象object的属性或者方法。如果获取对象的方法,则返回的是该方法的内存地址。那么,如需要运行这个方法,在后面添加一对括号即可。这跟使用函数指针是类似的。举个例子:

    • foo.py
     1 #!/usr/bin/python
     2 
     3 import sys
     4 
     5 
     6 class Person(object):
     7     def __init__(self, name, age):
     8         self.name = name
     9         self.age = age
    10 
    11     def get_name(self):
    12         return self.name
    13 
    14     def get_age(self):
    15         return self.age
    16 
    17     def set_name(self, name):
    18         self.name = name
    19 
    20     def set_age(self, age):
    21         self.age = age
    22 
    23 
    24 def main(argc, argv):
    25     p = Person('Jack', 25)
    26     print 'Name: %s, Age: %d' % (p.get_name(), p.get_age())
    27 
    28     getattr(p, 'set_name')('Lily')
    29     getattr(p, 'set_age')(36)
    30     print 'Name: %s, Age: %d' % (getattr(p, 'get_name')(),
    31                                  getattr(p, 'get_age')())
    32 
    33     return 0
    34 
    35 if __name__ == '__main__':
    36     argv = sys.argv
    37     argc = len(sys.argv)
    38     sys.exit(main(argc, argv))
    • run foo.py
    $ ./foo.py 
    Name: Jack, Age: 25
    Name: Lily, Age: 36

    在上面的例子中,

    getattr(p, 'set_name')('Lily')

    等同于

    p.set_name('Lily')

    2. 一个利用getattr()精简代码的例子

     1 import time
     2 
     3 
     4 class JenkinsProject(object):
     5     def __get_job_prop(self, job, method, interval):
     6         for i in range(self.retry_cnt):
     7             try:
     8                 return getattr(job, method)()
     9             except Exception as e:
    10                 time.sleep(interval)
    11 
    12         print("fail to %s after retry %d times" %
    13               (method.replace('_', ' '), self.retry_cnt))
    14         raise e
    15 
    16     def get_build_ids(self, job, interval=60):
    17         return self.__get_job_prop(self, job, 'get_build_ids', interval)
    18 
    19     def get_last_build(self, job, interval=60):
    20         return self.__get_job_prop(self, job, 'get_last_build', interval)
  • 相关阅读:
    简明python教程九----异常
    简明python教程八----输入/输出
    简明python教程七----面向对象的编程(下)
    vue --- 生命周期
    es6 ----- export 和 import
    jq ---- 实现浏览器全屏
    Vue项目自动转换 px 为 rem,高保真还原设计图
    vue ---- 实现手机端(左滑 删除。右划 正常)
    js---- localStorage的基本用法
    点击 下载文件保存
  • 原文地址:https://www.cnblogs.com/idorax/p/9414577.html
Copyright © 2011-2022 走看看