Python中的反射
2016-05-16 15:40
543 查看
fun = getattr(str,'__len__')
print fun('abc') #相当于执行 'abc'.__len__
print fun('abc') #相当于执行 'abc'.__len__
class abc(object): def get(self,name): print name + " hello" def n(self,arg): ags = arg.split('.') #print ags #返回['get', 'iiii'] fun = getattr(self,ags[0]) #ags[0]==get,该class中包含有get方法 fun(ags[1]) #相当于执行 get('iiii') p=abc() print p.n('get.iiii') #通过反射,可以直接将get识别为class abc中的方法,iiii为get方法中的参数
相关文章推荐
- 安装PIL库时提示python未注册错误(自定义python安装路径)
- 如何进行 Python性能分析,你才能如鱼得水?
- [Python]第六讲:tuple
- [Python]第五讲:list
- 如何进行 Python性能分析,你才能如鱼得水?
- Python-open读写模式
- python操作字典类型的常用方法(推荐)
- Python:正则表达式
- PYTHON-基础-时间日期处理小结
- python核心编练习答案
- python字典的常用操作方法小结
- 使用python读取大文件
- 使用python读取大文件
- 使用python读取大文件
- ArcGIS 字段编辑器,编写简单python代码
- python cross-platform note
- 在Windows10 64位 Anaconda4 Python3.5下安装XGBoost
- python字典的常用操作方法
- python3下载百度图片
- Python笔记