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 相关文章推荐
PHP webshell检查工具 python实现代码
Sep 15 Python
基于Python实现对PDF文件的OCR识别
Aug 05 Python
python中子类调用父类函数的方法示例
Aug 18 Python
使用Python制作微信跳一跳辅助
Jan 31 Python
Python代码太长换行的实现
Jul 05 Python
pyqt5中动画的使用详解
Apr 01 Python
Windows+Anaconda3+PyTorch+PyCharm的安装教程图文详解
Apr 03 Python
python 实现朴素贝叶斯算法的示例
Sep 30 Python
python中doctest库实例用法
Dec 31 Python
Python爬取英雄联盟MSI直播间弹幕并生成词云图
Jun 01 Python
Python+Appium自动化测试的实战
Jun 30 Python
使用python将HTML转换为PDF pdfkit包(wkhtmltopdf) 的使用方法
Apr 21 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中json_encode处理gbk与gb2312中文乱码问题的解决方法
2014/07/10 PHP
php die()与exit()的区别实例详解
2016/12/03 PHP
PHP+JavaScript实现无刷新上传图片
2017/02/21 PHP
javascript+mapbar实现地图定位
2010/04/09 Javascript
如何确保JavaScript的执行顺序 之jQuery.html深度分析
2011/03/03 Javascript
如何获取JQUERY AJAX返回的JSON结果集实现代码
2012/12/10 Javascript
JavaScript对象学习经验整理
2013/10/12 Javascript
限制上传文件大小和格式的jQuery插件实例
2015/01/24 Javascript
Node.js抓取中文网页乱码问题和解决方法
2015/02/10 Javascript
JavaScript面对国际化编程时的一些建议
2015/06/24 Javascript
jQuery实现分隔条左右拖动功能
2015/11/21 Javascript
jQuery fancybox在ie浏览器下无法显示关闭按钮的解决办法
2016/02/19 Javascript
AngularJS控制器详解及示例代码
2016/08/16 Javascript
JS判断输入的字符串是否是数字的方法(正则表达式)
2016/11/29 Javascript
函数四种调用模式以及其中的this指向
2017/01/16 Javascript
Vue计算属性的学习笔记
2017/03/22 Javascript
ES6中Array.find()和findIndex()函数的用法详解
2017/09/16 Javascript
为什么Vue3.0使用Proxy实现数据监听(defineProperty表示不背这个锅)
2019/10/14 Javascript
python client使用http post 到server端的代码
2013/02/10 Python
用Python抢过年的火车票附源码
2015/12/07 Python
Python的组合模式与责任链模式编程示例
2016/02/02 Python
Python中scatter函数参数及用法详解
2017/11/08 Python
解决python中 f.write写入中文出错的问题
2018/10/31 Python
Tensorflow限制CPU个数实例
2020/02/06 Python
canvas三角函数模拟水波效果的示例代码
2018/07/03 HTML / CSS
Guess美国官网:美国知名服装品牌
2019/04/08 全球购物
Viking Direct爱尔兰:办公用品和家具
2019/11/21 全球购物
经济类毕业生求职信
2014/06/26 职场文书
2015年教师师德师风承诺书
2015/04/28 职场文书
幼儿园安全教育月活动总结
2015/05/08 职场文书
芙蓉镇观后感
2015/06/10 职场文书
护士业务学习心得体会
2016/01/25 职场文书
调解协议书范本
2016/03/21 职场文书
小学生必读成语故事大全:送给暑假的你们
2019/07/09 职场文书
2019通用版劳动合同范本!
2019/07/11 职场文书
MySQL删除和插入数据很慢的问题解决
2021/06/03 MySQL