详细讲述python自省函数getattr的用法
来源:互联网 发布:股票交易策略 知乎 编辑:程序博客网 时间:2024/04/28 04:00
这两天看了公司的老代码,发现有大量的在用getattr…. 虽然在不得已的时候,也会考虑用getattr,只是没这么频繁而已….
这边的getattr主要是用在函数调用上,比如一个简单的rpc服务,里面注册了N个函数,这时候,客户端是知道自己需要调用哪个函数的,他给我传递了一个名字叫getName函数名,那么我用getattr直接调用就OK了….
关于python内建函数getattr的用法,文章出处 http://xiaorui.cc/?p=1798
getattr是python里的一个内建函数,在python的官方文档中: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()这个方法最主要的作用是实现反射机制。也就是说可以通过字符串获取方法实例。这样,你就可以把一个类可能要调用的方法放在配置文件里,在需要的时候动态加载。
python里面跟getattr相关的有hasattr,setattr,delattr ,那么我们通过下面的例子,来详细的说说他们的用法。
Python
#!/usr/bin/pythonclass Xiaorui: def __init__(self): self.name = ‘fengyun’ def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print “Hello,i’m %s”%self.name foo = Xiaorui()
一. hasattr(object,name)
bool 判断object中是否具有name属性,例如:
foo = Xiaorui()
hasattr(foo,’setName’) #判断setName是否存在,存在则返回True。
二. getattr(object,name,default)
如果存在name属性(方法)则返回name的值(方法地址)否则返回default值。
getattr(foo,’name’,’NA’) #存在name属性,所以返回其value
‘fengyun’
getattr(foo,’age’,’NA’)
‘NA’
一会发现,他其实跟直接object.name 用法一样。
三. setattr(object,name,default)
setattr(foo,’age’,’18’) #
字符串可能会列出一个现有的属性(或一个新的属性)。这个函数将值赋给属性的
.类似foo.age = 18
getattr(foo,’age’,’not find’)
’18’
setattr(foo,’work’,’student’) #
可以用setattr方法,把foo的work变量,设置为student
getattr(foo,’work’,’not find’)
‘student’
四. delattr(object,’name’)
delattr(foo,’name’)#删除属性name,原值为‘fengyun’
getattr(foo,’name’,’not find’)
‘not find’
今重点是getattr的用法, 我也不多说了,就简单写个例子,说明下,我一般是怎么用getattr的。
Python
#xiaorui.ccimport testimport multiprocessingdef run(func,*args):# print getattr(test, "hello")("hello", "world") print getattr(test, func)(*args)pool = multiprocessing.Pool(processes=4)for i in xrange(10): pool.apply_async(run, ('hello','hello','world' )) pool.apply_async(run, ('test_sleep','hello','world' ))pool.close()pool.join()print "END from xiaorui.cc"
文件名是, test.py
Python
import timedef hello(str1, str2): print 'hello' return [str1, str2]def test_sleep(str1, str2): print 'test_sleep' time.sleep(3) return [str1, str2]
- 详细讲述python自省函数getattr的用法
- 详细讲述python自省函数getattr的用法
- Python的getattr用法
- Python的getattr函数
- python 的getattr的用法
- python常用的自省函数
- python的自省机制以及dir() | hasattr() | getattr() | setattr()的使用
- python进阶(4) getattr的用法
- python getattr/setattr/hasattr的用法
- python hasattr() getattr() setattr()函数的使用
- Python的hasattr() getattr() setattr() 函数使用方法
- python 内置函数getattr
- python自省函数的总结(源码剖析)
- Python的getattr()
- Python的hasattr、getattr
- 神奇的getattr - Python
- Python中的getattr()函数 (一)
- Python中的getattr()函数 (二)
- Bitmap的简单保存
- automake manul
- IOS学习开发路线图
- Linux下 tomcat/mysql相关
- uva 1589 xiangqi
- 详细讲述python自省函数getattr的用法
- The request sent by the client was syntactically incorrect ()问题
- 2015-12-7 15:29:40
- 1052. Linked List Sorting (25)【链表+排序】——PAT (Advanced Level) Practise
- 【Leetcode】Find the Duplicate Number
- 如何从官网下载最新的jdk API
- How to use NQ logs to check data in DB
- 如何正确获得Android内外SD卡路径
- [转载文章] 解析漏洞总结