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程序设计有所帮助。

相关文章

  • TensorFlow MNIST手写数据集的实现方法

    TensorFlow MNIST手写数据集的实现方法

    MNIST数据集中包含了各种各样的手写数字图片,这篇文章主要介绍了TensorFlow MNIST手写数据集的实现方法,需要的朋友可以参考下
    2020-02-02
  • 如何在Python中利用matplotlib.pyplot画出函数图详解

    如何在Python中利用matplotlib.pyplot画出函数图详解

    通过图像可以直观地学习函数变化、分布等规律,在学习函数、概率分布等方面效果显著,下面这篇文章主要给大家介绍了关于如何在Python中利用matplotlib.pyplot画出函数图的相关资料,需要的朋友可以参考下
    2022-08-08
  • Python3.4编程实现简单抓取爬虫功能示例

    Python3.4编程实现简单抓取爬虫功能示例

    这篇文章主要介绍了Python3.4编程实现简单抓取爬虫功能,涉及Python3.4网页抓取及正则解析相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • python 使用get_argument获取url query参数

    python 使用get_argument获取url query参数

    这篇文章主要介绍了python 使用get_argument获取url query参数的相关资料,需要的朋友可以参考下
    2017-04-04
  • Python pyecharts数据可视化实例详解

    Python pyecharts数据可视化实例详解

    PyEcharts是一个用于生成 Echarts图表的类库, Python是一门富有表达力的语言,很适合用于数据处理,下面这篇文章主要给大家介绍了关于Python pyecharts数据可视化的相关资料,需要的朋友可以参考下
    2022-05-05
  • Python列表常用函数使用详解

    Python列表常用函数使用详解

    这篇文章主要为大家介绍了Python列表常用的一些函数的使用详解,并通过一些简单的案例让大家更快的理解,感兴趣的可以跟随小编一起学习一下
    2021-12-12
  • Pygame改编飞机大战制作兔子接月饼游戏

    Pygame改编飞机大战制作兔子接月饼游戏

    一年中秋又快到了,今年加入了Python的学习行列,得益于Python的开发效率和易读性,网上写文章的次数多了起来,既然是中秋节那肯定要搞个应景的游戏才行
    2022-09-09
  • python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法

    python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法

    这篇文章给大家分享了关于python安装PIL模块时遇到Unable to find vcvarsall.bat错误的解决方法,相信会对不少人有一定的参考借鉴价值。有需要的朋友们下面来一起看看吧。
    2016-09-09
  • QML使用Python的函数过程解析

    QML使用Python的函数过程解析

    这篇文章主要介绍了QML使用Python的函数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python ansible服务及剧本编写

    python ansible服务及剧本编写

    python语言是运维人员必会的语言,而ansible是一个基于Python开发的自动化运维工具 (saltstack)。其功能实现基于SSH远程连接服务;ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能
    2017-12-12

最新评论