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 搭建Web站点之Web服务器网关接口
Nov 06 Python
CentOS中升级Python版本的方法详解
Jul 10 Python
python使用tcp实现局域网内文件传输
Mar 20 Python
Python中super函数用法实例分析
Mar 18 Python
python将字符串转换成json的方法小结
Jul 09 Python
Python OrderedDict的使用案例解析
Oct 25 Python
Docker部署Python爬虫项目的方法步骤
Jan 19 Python
tensorflow之并行读入数据详解
Feb 05 Python
Numpy 理解ndarray对象的示例代码
Apr 03 Python
python3将变量输入的简单实例
Aug 19 Python
一行Python命令实现批量加水印
Apr 07 Python
python数字图像处理之图像的批量处理
Jun 28 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中读取和写入WORD文档的代码
2008/04/09 PHP
codeigniter集成ucenter1.6双向通信的解决办法
2014/06/12 PHP
浅析iis7.5安装配置php环境
2015/05/10 PHP
js中Number数字数值运算后值不对的解决方法
2017/02/28 Javascript
详解Vue2 无限级分类(添加,删除,修改)
2017/03/07 Javascript
基于VUE移动音乐WEBAPP跨域请求失败的解决方法
2018/01/16 Javascript
Vue 组件修改根实例的数据的方法
2019/04/02 Javascript
微信小程序实现蓝牙打印
2019/09/23 Javascript
Vue v-for循环之@click点击事件获取元素示例
2019/11/09 Javascript
vue radio单选框,获取当前项(每一项)的value值操作
2020/09/10 Javascript
python 多线程应用介绍
2012/12/19 Python
python获取各操作系统硬件信息的方法
2015/06/03 Python
Python 中的with关键字使用详解
2016/09/11 Python
Python自定义进程池实例分析【生产者、消费者模型问题】
2016/09/19 Python
Python实现矩阵加法和乘法的方法分析
2017/12/19 Python
Python的numpy库中将矩阵转换为列表等函数的方法
2018/04/04 Python
Python在图片中插入大量文字并且自动换行
2019/01/02 Python
python 计算一个字符串中所有数字的和实例
2019/06/11 Python
pytorch 在sequential中使用view来reshape的例子
2019/08/20 Python
Python实现微信中找回好友、群聊用户撤回的消息功能示例
2019/08/23 Python
详解numpy矩阵的创建与数据类型
2019/10/18 Python
python 正则表达式参数替换实例详解
2020/01/17 Python
安装完Python包然后找不到模块的解决步骤
2020/02/13 Python
python中count函数知识点浅析
2020/12/17 Python
HTML5实现应用程序缓存(Application Cache)
2020/06/16 HTML / CSS
西班牙太阳镜品牌:Hawkers
2018/03/11 全球购物
西班牙在线药店:DosFarma
2020/03/28 全球购物
解释一下钝化(Swap out)
2016/12/26 面试题
Python中如何定义一个函数
2016/09/06 面试题
优秀员工自荐书
2013/12/19 职场文书
英语教学随笔感言
2014/02/20 职场文书
职业女性的职业规划
2014/03/04 职场文书
团支部推优材料
2014/05/21 职场文书
新品发布会策划方案
2014/06/08 职场文书
2015年小学中秋节活动总结
2015/03/23 职场文书
2015年村党支部工作总结
2015/04/30 职场文书