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模拟鼠标拖动操作的方法
Mar 11 Python
python根据出生日期返回年龄的方法
Mar 26 Python
【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
Nov 19 Python
浅谈django model的get和filter方法的区别(必看篇)
May 23 Python
Python爬虫中urllib库的进阶学习
Jan 05 Python
Python matplotlib绘图可视化知识点整理(小结)
Mar 16 Python
Python基础之循环语句用法示例【for、while循环】
Mar 23 Python
python dataframe NaN处理方式
Dec 26 Python
selenium 多窗口切换的实现(windows)
Jan 18 Python
提高python代码运行效率的一些建议
Sep 29 Python
selenium自动化测试入门实战
Dec 21 Python
Python中的xlrd模块使用整理
Jun 15 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
Linux下CoreSeek及PHP扩展模块的安装
2012/09/23 PHP
php实现用于删除整个目录的递归函数
2015/03/16 PHP
php获取字符串前几位的实例(substr返回字符串的子串用法)
2017/03/08 PHP
laravel 解决路由除了根目录其他都404的问题
2019/10/18 PHP
JavaScript 异步调用框架 (Part 5 - 链式实现)
2009/08/04 Javascript
js输出列表实现代码
2010/09/12 Javascript
window.name代替cookie的实现代码
2010/11/28 Javascript
AspNet中使用JQuery上传插件Uploadify详解
2015/05/20 Javascript
JavaScript生成SQL查询表单的方法
2015/08/13 Javascript
AngularJS实现元素显示和隐藏的几个案例
2015/12/09 Javascript
Javascript中判断一个值是否为undefined的方法详解
2016/09/28 Javascript
Vue系列:通过vue-router如何传递参数示例
2017/01/16 Javascript
jQuery实现淡入淡出的模态框
2017/02/09 Javascript
jQuery实现动态删除LI的方法
2017/05/30 jQuery
你应该了解的JavaScript Array.map()五种用途小结
2018/11/14 Javascript
微信小程序实现打卡签到页面
2020/09/21 Javascript
如何在面试中手写出javascript节流和防抖函数
2020/10/22 Javascript
[05:16]《大圣!大圣》——DOTA2新英雄齐天大圣配音李世宏老师专访
2016/12/13 DOTA
Python最长公共子串算法实例
2015/03/07 Python
详细解析Python中__init__()方法的高级应用
2015/05/11 Python
Python处理字符串之isspace()方法的使用
2015/05/19 Python
Python图片裁剪实例代码(如头像裁剪)
2017/06/21 Python
python flask 多对多表查询功能
2017/06/25 Python
Python使用pandas处理CSV文件的实例讲解
2018/06/22 Python
python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
2018/09/13 Python
python把1变成01的步骤总结
2019/02/27 Python
python3中property使用方法详解
2019/04/23 Python
PyCharm 解决找不到新打开项目的窗口问题
2021/01/15 Python
CSS3实现类似翻书效果的过渡动画的示例代码
2019/09/06 HTML / CSS
数学与统计学院学生个人职业生涯规划书
2014/02/10 职场文书
体育比赛口号
2014/06/09 职场文书
动物科学专业求职信
2014/07/27 职场文书
2019自荐信该如何写呢?
2019/07/05 职场文书
Python编写可视化界面的全过程(Python+PyCharm+PyQt)
2021/05/17 Python
一次SQL查询优化原理分析(900W+数据从17s到300ms)
2022/06/10 SQL Server
Python+SeaTable实现计算两个日期间的工作日天数
2022/07/07 Python