[英]How can a subclass be created from a string passed to the superclass in python?
[英]python subclass can't import superclass from superclass
首先,我很可能犯一个可怕的错误。 但是,走吧!
我的超类(project / src / mlbase.py)
from preprocessing import PreProcessing
class MLBase:
preProcessing = None
def __init__(self,preprocessingOptions):
self.preProcessing = PreProcessing(preProcessingOptions)
# and more stuff here...
我的子类(project / src / preprocessing.py)
from mlbase import MLBase
class PreProcessing(MLBase):
def __init__(self,options):
#processing options here...
pass
我的脚本实例化了一切(project / main.py)
from src.mlbase import MLBase
mlb = MLBase(preProcessingOptions = {})
DIRS
"""
project
|
+ src
|
+ mlbase.py
|
+ preprocessing.py
|
+ main.py
"""
如你看到的。 目的是要实例化超类的子类。 但是当src/preprocessing.py
模块尝试从src.mlbase.py
导入MLBase
类时,我收到以下错误:
ImportError:无法导入名称MLBase
为什么会这样呢?
这只是一个错字。 您声明了class MBase
但尝试导入MLBase
。 您所要做的就是将超类文件更改为此:
from preprocessing import PreProcessing
class MLBase: #Note that it's "MLBase", not "MBase"
preProcessing = None
def __init__(self,preprocessingOptions):
self.preProcessing = PreProcessing(preProcessingOptions)
# and more stuff here...
解决方案是在构造函数方法中使用from preprocessing import PreProcessing
导入Import PreProcessing类! 我不知道为什么! 我真的很想了解!
在mlbase模块中:
class MLBase:
def __init__(self,preProcessingOptions):
from preprocessing import Preprocessing
# more stuff
在预处理模块中
from mlbase import MLBase
class PreProcessing(MLBase):
def __init__(self,preProcessingOptions):
# more stuff
对我来说太奇怪了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.