python中反射用法實(shí)例
更新時(shí)間:2015年03月27日 09:48:43 作者:songguo
這篇文章主要介紹了python中反射用法,實(shí)例分析了Python中反射的原理與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了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
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python靜態(tài)web服務(wù)器實(shí)現(xiàn)方法及代碼詳解
在本篇內(nèi)容里小編給大家分享了一篇關(guān)于python靜態(tài)web服務(wù)器實(shí)現(xiàn)方法,有需要的朋友們可以參考下。2022-11-11詳解python中的三種命令行模塊(sys.argv,argparse,click)
這篇文章主要介紹了python中的三種命令行模塊(sys.argv,argparse,click)的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12python playwright 自動(dòng)等待和斷言詳解
這篇文章主要為大家介紹了python playwright 自動(dòng)等待和斷言,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-11-11基于Python+Pygame實(shí)現(xiàn)變異狗大戰(zhàn)游戲
只有你想不到,沒有我找不到寫不了的好游戲!這篇文章就來(lái)和大家分享一下如何基于Python+Pygame實(shí)現(xiàn)變異狗大戰(zhàn)游戲,感興趣的可以了解一下2023-03-03pycharm 中mark directory as exclude的用法詳解
今天小編就為大家分享一篇pycharm 中mark directory as exclude的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02