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中的类,对象,方法,属性
Sep 11 Python
Python安装第三方库及常见问题处理方法汇总
Sep 13 Python
Python实现模拟分割大文件及多线程处理的方法
Oct 10 Python
python3.4实现邮件发送功能
May 28 Python
PyTorch基本数据类型(一)
May 22 Python
python反转列表的三种方式解析
Nov 08 Python
python保留小数位的三种实现方法
Jan 07 Python
TensorFlow的reshape操作 tf.reshape的实现
Apr 19 Python
PyTorch 中的傅里叶卷积实现示例
Dec 11 Python
Jupyter Notebook 远程访问配置详解
Jan 11 Python
Python类方法总结讲解
Jul 26 Python
python3操作redis实现List列表实例
Aug 04 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打造的tab选项卡效果代码(不用js)
2010/12/29 PHP
Look And Say 序列php实现代码
2011/05/22 PHP
解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)
2013/06/18 PHP
PHP实现对数字分隔加千分号的方法
2019/03/18 PHP
javascript jQuery插件练习
2008/12/24 Javascript
script标签属性type与language使用选择
2012/12/02 Javascript
js实现在页面上弹出蒙板技巧简单实用
2013/04/16 Javascript
JavaScript中this关键词的使用技巧、工作原理以及注意事项
2014/05/20 Javascript
JS辨别访问浏览器判断是android还是ios系统
2014/08/19 Javascript
IE中getElementsByName()对有些元素无效的解决方案
2014/09/28 Javascript
JQuery插入DOM节点的方法
2015/06/11 Javascript
原生js配合cookie制作保存路径的拖拽
2015/12/29 Javascript
jQuery获取当前点击的对象元素(实现代码)
2016/05/19 Javascript
JavaScript_ECMA5数组新特性详解
2016/06/12 Javascript
浅谈jQuery效果函数
2016/09/16 Javascript
浅谈 vue 中的 watcher
2017/12/04 Javascript
vue+springboot前后端分离实现单点登录跨域问题解决方法
2018/01/30 Javascript
Vue项目打包部署到iis服务器的配置方法
2019/10/14 Javascript
vue实现评论列表功能
2019/10/25 Javascript
JavaScript编码小技巧分享
2020/09/17 Javascript
利用node.js开发cli的完整步骤
2020/12/29 Javascript
[01:00]DOTA2 store: Collection of Artisan's Wonders
2015/08/12 DOTA
[59:32]Liquid vs Fnatic 2019国际邀请赛淘汰赛败者组BO1 8.20.mp4
2020/07/19 DOTA
Python的多态性实例分析
2015/07/07 Python
Python读取Json字典写入Excel表格的方法
2018/01/03 Python
python批量获取html内body内容的实例
2019/01/02 Python
python面向对象 反射原理解析
2019/08/12 Python
Python3标准库之dbm UNIX键-值数据库问题
2020/03/24 Python
keras load model时出现Missing Layer错误的解决方式
2020/06/11 Python
JDO的含义
2012/11/17 面试题
机电专业毕业生推荐信
2013/11/10 职场文书
入股协议书
2014/04/14 职场文书
文明寝室标语
2014/06/13 职场文书
天地会口号
2014/06/17 职场文书
房屋登记授权委托书范本
2014/10/09 职场文书
Spring Data JPA框架的核心概念和Repository接口
2022/04/28 Java/Android