在python中,我必须实例化某些类,在字符串中知道其名称,但是该类“存在”于动态导入的模块中。下面是一个示例:
加载程序类脚本:
import sys
class loader:
def __init__(self, module_name, class_name): # both args are strings
try:
__import__(module_name)
modul = sys.modules[module_name]
instance = modul.class_name() # obviously this doesn't works, here is my main problem!
except ImportError:
# manage import error
一些动态加载的模块脚本:
class myName:
# etc...
我使用这种安排使dyn-loaded-modules中的某些预定义行为使加载程序类使用任何动态加载的模块...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句