python動態(tài)加載包的方法小結(jié)
本文實例總結(jié)了python動態(tài)加載包的方法。分享給大家供大家參考,具體如下:
動態(tài)加載模塊有三種方法
1. 使用系統(tǒng)函數(shù)__import_()
stringmodule = __import__('string')
2. 使用imp 模塊
import imp
stringmodule = imp.load_module('string',*imp.find_module('string'))
imp.load_source("TYACMgrHandler_"+app.upper(), filepath)
3. 用exec
import_string = "import string as stringmodule" exec import_string
變量是否存在
1. hasattr(Test,'t')
2. 'var' in locals().keys()
3. 'var' in dir()
4. vars().has_key('s')
動態(tài)增加屬性
class Obj(object):
pass
def main():
list=["a","b", "c"]
for i inrange(1,len(list),2):
Obj = type('Obj',(),{list[i]:lambdaself,s:obj.__setattr__(s.split(" = ")[0],s.split(" = ")[1])})
obj =Obj()
for i inrange(0,len(list),2):
obj.__setattr__(list[i],list[i])
obj.a =1
obj.b("a =2")
obj.b("c =3")
printobj.a
printobj.c
if __name__ == '__main__':
main()
動態(tài)載入包:
def test(s,e):
print s
print e
class C():
def __init__(self,name):
print name
def test(self):
print 'class!!!'
加載器代碼:
class Dynload():
def __init__(self,package,imp_list):
self.package=package
self.imp=imp_list
def getobject(self):
return __import__(self.package,globals(),locals(),self.imp,-1)
def getClassInstance(self,classstr,*args):
return getattr(self.getobject(),classstr)(*args)
def execfunc(self,method,*args):
return getattr(self.getobject(),method)(*args)
def execMethod(self,instance,method,*args):
return getattr(instance,method)(*args)
#Test:
dyn=Dynload('util.common',['*'])
ins=dyn.getClassInstance('C','gao')
dyn.execMethod(ins,'test')
dyn.execfunc('test','Hello','function!')
根據(jù)名字加載指定文件
def loadapp(self, app):
filepath="mgr/"+app+".py"
if os.path.exists(filepath):
imp.load_source("TYACMgrHandler_"+app.upper(), filepath)
//修改了app.py,從新調(diào)用這個函數(shù),新的代碼自動生效
根據(jù)名字調(diào)用對應(yīng)方法
return getattr(self, op)(args.get("port"), args) //op="start" args=dict
getattr(self, self.request.method.lower())(*args, **kwargs)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python GUI庫圖形界面開發(fā)之PyQt5選項卡控件QTabWidget詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5選項卡控件QTabWidget詳細使用方法與實例,需要的朋友可以參考下2020-03-03
Python的json模塊中json.load()和json.loads()的區(qū)別
這篇文章主要介紹了Python的json模塊中json.load()和json.loads()的區(qū)別,json.load用于從一個文件對象中讀取JSON數(shù)據(jù)并將其解析為Python對象,而json.loads用于解析一個JSON格式的字符串并將其轉(zhuǎn)換為Python對象,根據(jù)你的具體需求選擇使用哪個方法,需要的朋友可以參考下2024-12-12
Python tkinter的grid布局及Text動態(tài)顯示方法
今天小編就為大家分享一篇Python tkinter的grid布局及Text動態(tài)顯示方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python?sklearn數(shù)據(jù)預(yù)處理之數(shù)據(jù)縮放詳解
數(shù)據(jù)的預(yù)處理是數(shù)據(jù)分析,或者機器學(xué)習(xí)訓(xùn)練前的重要步驟,這篇文章主要為大家詳細介紹了sklearn數(shù)據(jù)預(yù)處理中數(shù)據(jù)縮放的相關(guān)知識,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10
Pygame游戲開發(fā)之太空射擊實戰(zhàn)盾牌篇
相信大多數(shù)8090后都玩過太空射擊游戲,在過去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來自己動手實現(xiàn)它,在編寫學(xué)習(xí)中回顧過往展望未來,在本課中,我們將為玩家添加一個盾牌以及一個用于顯示盾牌等級的欄2022-08-08
Python中flatten( ),matrix.A用法說明
這篇文章主要介紹了Python中flatten( ),matrix.A用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
基于python使用Pillow做動態(tài)圖在圖中生成二維碼以及圖像處理
這篇文章主要介紹了基于python使用Pillow做動態(tài)圖在圖中生成二維碼以及圖像處理,分享pillow的一些簡單使用,喜歡的話大家可以參考文章內(nèi)容下去試試奧2022-02-02

