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

 

转载于:https://www.cnblogs.com/idorax/p/9414577.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值