Python中動態(tài)創(chuàng)建類實(shí)例的方法
簡介
在Java中我們可以通過反射來根據(jù)類名創(chuàng)建類實(shí)例,那么在Python我們怎么實(shí)現(xiàn)類似功能呢?
其實(shí)在Python有一個builtin函數(shù)import,我們可以使用這個函數(shù)來在運(yùn)行時動態(tài)加載一些模塊。如下:
def createInstance(module_name, class_name, *args, **kwargs): module_meta = __import__(module_name, globals(), locals(), [class_name]) class_meta = getattr(module_meta, class_name) obj = class_meta(*args, **kwargs) return obj
例子
首先我們建一個目錄 my_modules,其中包括三個文件
* init.py: 模塊文件
* my_module.py: 測試用的模塊
* my_another_module: 另一個測試用的模塊
my_module.py
from my_modules.my_another_module import *
class MyObject(object):
def test(self):
print 'MyObject.test'
MyObject1().test()
MyObject2().test()
MyAnotherObject().test()
class MyObject1(object):
def test(self):
print 'MyObject1.test'
class MyObject2(object):
def test(self):
print 'MyObject2.test'
my_another_module.py
class MyAnotherObject(object):
def test(self):
print 'MyAnotherObject.test'
test.py
def createInstance(module_name, class_name, *args, **kwargs):
module_meta = __import__(module_name, globals(), locals(), [class_name])
class_meta = getattr(module_meta, class_name)
obj = class_meta(*args, **kwargs)
return obj
obj = createInstance("my_modules.my_module", "MyObject")
obj.test()
MyObject.test
MyObject1.test
MyObject2.test
MyAnotherObject.test
pyinstaller集成
對于使用pyinstaller打包的應(yīng)用程序,如果使用上面的代碼,運(yùn)行打包后的程序會出現(xiàn)下面的錯誤
Traceback (most recent call last):
File "test.py", line 12, in <module>
obj = createInstance("my_modules.my_module", "MyObject")
File "test.py", line 7, in createInstance
module_meta = __import__(module_name, globals(), locals(), [class_name])
ImportError: No module named my_modules.my_module
Failed to execute script test
這里錯誤的原因是 pyinstaller 在打包分析類的時候沒有分析到 my_modules 下面的模塊,所以運(yùn)行報錯。
解決辦法一:
在 test.py 中把 my_modules 下的模塊手動 import,見下面代碼中的第一行。這種方法最簡單,但是顯然不太好。
import my_modules.my_module
def createInstance(module_name, class_name, *args, **kwargs):
module_meta = __import__(module_name, globals(), locals(), [class_name])
class_meta = getattr(module_meta, class_name)
obj = class_meta(*args, **kwargs)
return obj
obj = createInstance("my_modules.my_module", "MyObject")
obj.test()
解決辦法二:
在使用 pyinstaller 打包的時候,指定 “–hidden-import”,如下
pyinstaller -D --hidden-import my_modules.my_module test.py
解決辦法三:
動態(tài)修改 python 運(yùn)行時path,見下面代碼中的前兩行,其中path我們可以通過環(huán)境變量或者參數(shù)傳遞進(jìn)來。顯然這種方法要比前兩種方法靈活的多。
import sys
sys.path.append(...)
def createInstance(module_name, class_name, *args, **kwargs):
module_meta = __import__(module_name, globals(), locals(), [class_name])
class_meta = getattr(module_meta, class_name)
obj = class_meta(*args, **kwargs)
return obj
obj = createInstance("my_modules.my_module", "MyObject")
obj.test()
以上所述是小編給大家介紹的Python中動態(tài)創(chuàng)建類實(shí)例的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- python實(shí)現(xiàn)動態(tài)創(chuàng)建類的方法分析
- python 動態(tài)獲取當(dāng)前運(yùn)行的類名和函數(shù)名的方法
- Python實(shí)現(xiàn)動態(tài)添加類的屬性或成員函數(shù)的解決方法
- Python實(shí)現(xiàn)動態(tài)加載模塊、類、函數(shù)的方法分析
- 在python的類中動態(tài)添加屬性與生成對象
- Python類的動態(tài)修改的實(shí)例方法
- python 類對象和實(shí)例對象動態(tài)添加方法(分享)
- python類的方法屬性與方法屬性的動態(tài)綁定代碼詳解
- Python內(nèi)置函數(shù)Type()函數(shù)一個有趣的用法
- 詳解Python中的type()方法的使用
- Python使用type動態(tài)創(chuàng)建類操作示例
相關(guān)文章
使用tensorflow實(shí)現(xiàn)AlexNet
這篇文章主要為大家詳細(xì)介紹了使用tensorflow實(shí)現(xiàn)AlexNet,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
python 簡單照相機(jī)調(diào)用系統(tǒng)攝像頭實(shí)現(xiàn)方法 pygame
今天小編就為大家分享一篇python 簡單照相機(jī)調(diào)用系統(tǒng)攝像頭實(shí)現(xiàn)方法 pygame,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Pytorch加載數(shù)據(jù)集的方式總結(jié)及補(bǔ)充
Pytorch自定義數(shù)據(jù)集方法,應(yīng)該是用pytorch做算法的最基本的東西,下面這篇文章主要給大家介紹了關(guān)于Pytorch加載數(shù)據(jù)集的方式總結(jié)及補(bǔ)充,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
python3實(shí)現(xiàn)用turtle模塊畫一棵隨機(jī)櫻花樹
今天小編就為大家分享一篇python3實(shí)現(xiàn)用turtle模塊畫一棵隨機(jī)櫻花樹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
解決Jupyter因卸載重裝導(dǎo)致的問題修復(fù)
這篇文章主要介紹了解決Jupyter因卸載重裝導(dǎo)致的問題修復(fù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

