python中反射用法实例


Posted in Python onMarch 27, 2015

本文实例讲述了python中反射用法。分享给大家供大家参考。具体如下:

import sys, types,new
def _get_mod(modulePath):
  try:
    aMod = sys.modules[modulePath]
    if not isinstance(aMod, types.ModuleType):
      raise KeyError
  except KeyError:
    # The last [''] is very important!
    aMod = __import__(modulePath, globals(), locals(), [''])
    sys.modules[modulePath] = aMod
  return aMod
def _get_func(fullFuncName):
  """Retrieve a function object from a full dotted-package name."""
  # Parse out the path, module, and function
  lastDot = fullFuncName.rfind(u".")
  funcName = fullFuncName[lastDot + 1:]
  modPath = fullFuncName[:lastDot]
  aMod = _get_mod(modPath)
  aFunc = getattr(aMod, funcName)
  # Assert that the function is a *callable* attribute.
  assert callable(aFunc), u"%s is not callable." % fullFuncName
  # Return a reference to the function itself,
  # not the results of the function.
  return aFunc
def _get_Class(fullClassName, parentClass=None):
  """Load a module and retrieve a class (NOT an instance).
  If the parentClass is supplied, className must be of parentClass
  or a subclass of parentClass (or None is returned).
  """
  aClass = _get_func(fullClassName)
  # Assert that the class is a subclass of parentClass.
  if parentClass is not None:
    if not issubclass(aClass, parentClass):
      raise TypeError(u"%s is not a subclass of %s" %
              (fullClassName, parentClass))
  # Return a reference to the class itself, not an instantiated object.
  return aClass
def applyFuc(obj,strFunc,arrArgs):
  objFunc = getattr(obj, strFunc)
  return apply(objFunc,arrArgs)
def getObject(fullClassName):
  clazz = _get_Class(fullClassName)
  return clazz()
if __name__=='__main__':
  aa=getObject("inetservices.services.company.Company")  
  bb=applyFuc(aa, "select", ['select * from ngsys2',None])
  print bb

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
深入Python函数编程的一些特性
Apr 13 Python
Python实现的数据结构与算法之队列详解
Apr 22 Python
Python中for循环控制语句用法实例
Jun 02 Python
对Tensorflow中权值和feature map的可视化详解
Jun 14 Python
Python函数参数匹配模型通用规则keyword-only参数详解
Jun 10 Python
关于 Python opencv 使用中的 ValueError: too many values to unpack
Jun 28 Python
python调用matplotlib模块绘制柱状图
Oct 18 Python
python 元组的使用方法
Jun 09 Python
使用Pytorch搭建模型的步骤
Nov 16 Python
4种非常实用的python内置数据结构
Apr 28 Python
如何在pycharm中快捷安装pip命令(如pygame)
May 31 Python
Python使用BeautifulSoup4修改网页内容
May 20 Python
Python中使用摄像头实现简单的延时摄影技术
Mar 27 #Python
python根据出生日期返回年龄的方法
Mar 26 #Python
python获取远程图片大小和尺寸的方法
Mar 26 #Python
python使用cStringIO实现临时内存文件访问的方法
Mar 26 #Python
python使用pil生成缩略图的方法
Mar 26 #Python
python实现基于两张图片生成圆角图标效果的方法
Mar 26 #Python
python正则表达式match和search用法实例
Mar 26 #Python
You might like
php中用date函数获取当前时间有误的解决办法
2013/08/02 PHP
CodeIgniter分页类pagination使用方法示例
2016/03/28 PHP
PHP基于curl模拟post提交json数据示例
2018/06/22 PHP
PHP7.1实现的AES与RSA加密操作示例
2018/06/15 PHP
PHP日志LOG类定义与用法示例
2018/09/06 PHP
laravel 字段格式化 modle 字段类型转换方法
2019/09/30 PHP
jquery监听div内容的变化具体实现思路
2013/11/04 Javascript
Javascript中各种trim的实现详细解析
2013/12/10 Javascript
JavaScript中的console.log()函数详细介绍
2014/12/29 Javascript
jQuery过滤选择器详解
2015/01/13 Javascript
使用jQuery实现input数值增量和减量的方法
2015/01/24 Javascript
js实现鼠标滑过文字链接色彩变化的效果
2015/05/06 Javascript
C++中的string类的用法小结
2015/08/07 Javascript
JS获取html元素的标记名实现方法
2016/10/08 Javascript
简单三步实现报表页面集成天气
2016/12/15 Javascript
vue.js 实现图片本地预览 裁剪 压缩 上传功能
2018/03/01 Javascript
微信小程序实现两边小中间大的轮播效果的示例代码
2018/12/07 Javascript
一次让你了解全部JavaScript的作用域
2019/06/24 Javascript
解决layUI的页面显示不全的问题
2019/09/20 Javascript
Python开发如何在ubuntu 15.10 上配置vim
2016/01/25 Python
python万年历实现代码 含运行结果
2017/05/20 Python
无法使用pip命令安装python第三方库的原因及解决方法
2018/06/12 Python
python对列进行平移变换的方法(shift)
2019/01/10 Python
在Python中居然可以定义两个同名通参数的函数
2019/01/31 Python
python3 sorted 如何实现自定义排序标准
2020/03/12 Python
JAVA及PYTHON质数计算代码对比解析
2020/06/10 Python
python实现批处理文件
2020/07/28 Python
英国最大的电子零件及配件零售商:Partmaster
2017/04/24 全球购物
俄罗斯香水和化妆品购物网站:Л’Этуаль
2018/05/10 全球购物
德国咖啡批发商:Coffeefair
2019/08/26 全球购物
Madda Fella官网:美国冒险家服装品牌
2020/01/16 全球购物
CLR与IL分别是什么含义
2016/08/23 面试题
预备党员群众路线教育实践活动思想汇报2014
2014/10/25 职场文书
学校国庆节活动总结
2015/03/23 职场文书
股东大会通知
2015/04/24 职场文书
mysql事务对效率的影响分析总结
2021/10/24 MySQL