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实现的插入排序算法原理与用法实例分析
Nov 22 Python
Python3实现定时任务的四种方式
Jun 03 Python
python实现随机漫步方法和原理
Jun 10 Python
python实现自动化上线脚本的示例
Jul 01 Python
Python使用matplotlib 模块scatter方法画散点图示例
Sep 27 Python
Python异常模块traceback用法实例分析
Oct 22 Python
在Pytorch中计算卷积方法的区别详解(conv2d的区别)
Jan 03 Python
如何基于python实现归一化处理
Jan 20 Python
Jupyter打开图形界面并画出正弦函数图像实例
Apr 24 Python
PyCharm安装PyQt5及其工具(Qt Designer、PyUIC、PyRcc)的步骤详解
Nov 02 Python
Python中X[:,0]和X[:,1]的用法
May 10 Python
Python开发工具Pycharm的安装以及使用步骤总结
Jun 24 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 获取完整url地址
2008/12/20 PHP
MyEclipse常用配置图文教程
2014/09/11 PHP
PHP中的闭包(匿名函数)浅析
2015/02/07 PHP
php实现的简单日志写入函数
2015/03/31 PHP
PHP中获取文件创建日期、修改日期、访问时间的方法
2016/11/05 PHP
PHP版微信小店接口开发实例
2016/11/12 PHP
详解laravel安装使用Passport(Api认证)
2018/07/27 PHP
JS写的数字拼图小游戏代码[学习参考]
2008/10/29 Javascript
Bootstrap CSS组件之导航条(navbar)
2016/12/17 Javascript
angular仿支付宝密码框输入效果
2017/03/25 Javascript
Vue生命周期示例详解
2017/04/12 Javascript
详解使用nvm管理多版本node的方法
2017/08/30 Javascript
老生常谈JavaScript面向对象基础与this指向问题
2017/10/16 Javascript
layui 监听表格复选框选中值的方法
2018/08/15 Javascript
如何从零开始手写Koa2框架
2019/03/22 Javascript
AutoJs实现刷宝短视频的思路详解
2020/05/22 Javascript
Python函数式编程指南(二):从函数开始
2015/06/24 Python
python基础教程之Filter使用方法
2017/01/17 Python
Python数据分析之如何利用pandas查询数据示例代码
2017/09/01 Python
python和shell监控linux服务器的详细代码
2018/06/22 Python
Python使用try except处理程序异常的三种常用方法分析
2018/09/05 Python
图文详解python安装Scrapy框架步骤
2019/05/20 Python
Django Python 获取请求头信息Content-Range的方法
2019/08/06 Python
Python栈的实现方法示例【列表、单链表】
2020/02/22 Python
python nohup 实现远程运行不宕机操作
2020/04/16 Python
如何通过python实现IOU计算代码实例
2020/11/02 Python
宝信软件JAVA工程师面试经历
2012/08/19 面试题
本科生求职简历的自我评价
2013/10/21 职场文书
2014年感恩节活动策划方案
2014/10/06 职场文书
2015年幼儿园大班工作总结
2015/04/25 职场文书
2015年计算机教师工作总结
2015/07/22 职场文书
2016年感恩教师节活动总结
2016/04/01 职场文书
vue中三级导航的菜单权限控制
2021/03/31 Vue.js
python自动化之如何利用allure生成测试报告
2021/05/02 Python
mysql数据库入门第一步之创建表
2021/05/14 MySQL
MongoDB日志切割的三种方式总结
2021/09/15 MongoDB