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 相关文章推荐
Linux下编译安装MySQL-Python教程
Feb 02 Python
python实现将汉字转换成汉语拼音的库
May 05 Python
python超简单解决约瑟夫环问题
May 12 Python
python实现文件快照加密保护的方法
Jun 30 Python
python写一个md5解密器示例
Feb 23 Python
python3连接MySQL数据库实例详解
May 24 Python
Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】
Jan 05 Python
查看python安装路径及pip安装的包列表及路径
Apr 03 Python
Python TCPServer 多线程多客户端通信的实现
Dec 31 Python
新建文件时Pycharm中自动设置头部模板信息的方法
Apr 17 Python
python urllib和urllib3知识点总结
Feb 08 Python
手残删除python之后的补救方法
Jun 26 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指定截取字符串中的中英文或数字字符的实例分享
2016/03/18 PHP
thinkPHP3.2.3结合Laypage实现的分页功能示例
2018/05/28 PHP
JavaScript 常用函数
2009/12/30 Javascript
Javascript延迟执行实现方法(setTimeout)
2010/12/30 Javascript
js取得url地址参数实例
2013/02/22 Javascript
jquery使用jquery.zclip插件复制对象的实例教程
2013/12/04 Javascript
jquery操作select大全
2014/04/25 Javascript
javascript if条件判断方法小结
2014/05/17 Javascript
基于JavaScript实现一定时间后去执行一个函数
2015/12/14 Javascript
jquery实现右侧栏菜单选择操作
2016/03/04 Javascript
浅谈javascript中关于日期和时间的基础知识
2016/07/13 Javascript
轻松实现js弹框显示选项
2016/09/13 Javascript
Vuex利用state保存新闻数据实例
2017/06/28 Javascript
Vue的Flux框架之Vuex状态管理器
2017/07/30 Javascript
angular4应用中输入的最小值和最大值的方法
2019/05/17 Javascript
vue安装遇到的5个报错及解决方法
2019/06/12 Javascript
python分析apache访问日志脚本分享
2015/02/26 Python
Python中字符串对齐方法介绍
2015/05/21 Python
python模块简介之有序字典(OrderedDict)
2016/12/01 Python
浅谈python numpy中nonzero()的用法
2018/04/02 Python
Python绘制正余弦函数图像的方法
2018/08/28 Python
python用opencv批量截取图像指定区域的方法
2019/01/24 Python
Django 模型类(models.py)的定义详解
2019/07/19 Python
Python xmltodict模块安装及代码实例
2020/10/05 Python
Algenist奥杰尼官网:微藻抗衰老护肤品牌
2017/07/15 全球购物
Wolford法国官网:奥地利奢侈内衣品牌
2020/08/11 全球购物
《两只鸟蛋》教学反思
2014/02/10 职场文书
爱国卫生月实施方案
2014/02/21 职场文书
党员个人公开承诺书
2014/08/29 职场文书
毕业实习自我鉴定范文2014
2014/09/26 职场文书
村主任群众路线教育实践活动个人对照检查材料思想汇报
2014/10/01 职场文书
学生抄作业检讨书(2篇)
2014/10/17 职场文书
公司借条范本
2015/05/25 职场文书
自从在 IDEA 中用了热部署神器 JRebel 之后,开发效率提升了 10(真棒)
2021/06/26 Java/Android
mongodb数据库迁移变更的解决方案
2021/09/04 MongoDB
利用nginx搭建RTMP视频点播、直播、HLS服务器
2022/05/25 Servers