python中反射用法实例

 更新时间:2015年03月27日 09:48:43   作者:songguo  
这篇文章主要介绍了python中反射用法,实例分析了Python中反射的原理与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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 matlab库简单用法讲解

    python matlab库简单用法讲解

    在本篇文章里小编给大家整理了一篇关于python matlab库简单用法讲解内容,有需要的朋友们可以学习下。
    2020-12-12
  • Python常用断言函数实例汇总

    Python常用断言函数实例汇总

    这篇文章主要介绍了Python常用断言函数实例汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • Python利用FlashText算法实现替换字符串

    Python利用FlashText算法实现替换字符串

    FlashText算法是由 Vikash Singh 于2017年发表的大规模关键词替换算法,比正则表达式替换快M倍以上,这个M是需要替换的关键词数量,关键词越多,FlashText算法的优势就越明显。本文将详细这一算法,需要的可以参考一下
    2022-03-03
  • python3实现二叉树的遍历与递归算法解析(小结)

    python3实现二叉树的遍历与递归算法解析(小结)

    这篇文章主要介绍了python3实现二叉树的遍历与递归算法解析(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 在Python中使用MongoEngine操作数据库教程实例

    在Python中使用MongoEngine操作数据库教程实例

    这篇文章主要介绍了在Python中使用MongoEngine操作数据库教程实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • python能在浏览器能运行吗

    python能在浏览器能运行吗

    在本篇文章里小编给大家整理了关于python能否在浏览器能运行的相关知识点内容,有需要的朋友们可以学习下。
    2020-06-06
  • 基于python的汉字转GBK码实现代码

    基于python的汉字转GBK码实现代码

    今天想用python调用百度框计算的搜过结果,看到了URL里面的汉字用GBK编码,虽然可以直接在URL里面加入中文,之前也做过一个简体字转GBK码的python函数,但还是略嫌麻烦,今天改了一下
    2012-02-02
  • python实现图书借阅系统

    python实现图书借阅系统

    这篇文章主要为大家详细介绍了python实现图书借阅系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • 浅谈Python中的闭包

    浅谈Python中的闭包

    简单说,闭包就是根据不同的配置信息得到不同的结果。再来看看专业的解释:闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外。
    2015-07-07
  • Python操作Excel的学习笔记

    Python操作Excel的学习笔记

    这篇文章主要介绍了Python操作Excel的学习笔记,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02

最新评论