classA 在 Python 中没有属性“camera”错误
classA has no attribute `camera` error in Python
我正在使用一个大型 Python 库,其中包含大量文件和 类。我试图创建自己的 class 以防止代码重复。这是所有 classes 的基本概述。
# The Scene Class
class Scene(Container):
def __init__(self, **kwargs):
Container.__init__(self, **kwargs)
// Set some attribute values including...
self.camera = self.camera_class(**self.camera_config)
# The Camera Class
class MovingCamera(Camera):
def __init__(self, frame=None, **kwargs):
// Set up values some values and then...
Camera.__init__(self, **kwargs)
# The MovingCameraScene Class
from manimlib.camera.moving_camera import MovingCamera
from manimlib.scene.scene import Scene
class MovingCameraScene(Scene):
CONFIG = {
"camera_class": MovingCamera
}
def setup(self):
Scene.setup(self)
assert(isinstance(self.camera, MovingCamera))
self.camera_frame = self.camera.frame
return self
// More code. There is no __init()__ method in this class.
# My Custom Class the Inherits MovingCamera Scene
class Something(MovingCameraScene):
CONFIG = {
"alpha": "beta"
}
def __init__(self, **kwargs):
self.texts = []
def common_method(self):
texts = self.texts
for i in texts:
// Manipulates Boxes
# My Final Class that Inherits Previous Class
class classA(Something):
CONFIG={
"opt_a": "opt_b"
}
def construct(self):
texts = self.texts
texts.append('Apple')
.
.
.
.
self.common_method()
# This is another class that inherits `MovingCameraScene`
class classB(MovingCameraScene):
CONFIG={
"opt_a": "opt_b"
}
def construct(self):
texts = []
texts.append('Apple')
.
.
.
.
// More code that was in `common_method` of `Something` but directly pasted here.
还有很多 class 但希望这应该足够了。
当我尝试使用自己的 class (Something
) 作为 classA
的父级时,出现以下错误:
'classA' object has no attribute 'camera'
我在使用直接继承自 MovingCameraScene
的 classB
时没有收到此错误。
你没有在Something
中调用超级构造函数:
class Something(MovingCameraScene):
CONFIG = {
"alpha": "beta"
}
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.texts = []
我正在使用一个大型 Python 库,其中包含大量文件和 类。我试图创建自己的 class 以防止代码重复。这是所有 classes 的基本概述。
# The Scene Class
class Scene(Container):
def __init__(self, **kwargs):
Container.__init__(self, **kwargs)
// Set some attribute values including...
self.camera = self.camera_class(**self.camera_config)
# The Camera Class
class MovingCamera(Camera):
def __init__(self, frame=None, **kwargs):
// Set up values some values and then...
Camera.__init__(self, **kwargs)
# The MovingCameraScene Class
from manimlib.camera.moving_camera import MovingCamera
from manimlib.scene.scene import Scene
class MovingCameraScene(Scene):
CONFIG = {
"camera_class": MovingCamera
}
def setup(self):
Scene.setup(self)
assert(isinstance(self.camera, MovingCamera))
self.camera_frame = self.camera.frame
return self
// More code. There is no __init()__ method in this class.
# My Custom Class the Inherits MovingCamera Scene
class Something(MovingCameraScene):
CONFIG = {
"alpha": "beta"
}
def __init__(self, **kwargs):
self.texts = []
def common_method(self):
texts = self.texts
for i in texts:
// Manipulates Boxes
# My Final Class that Inherits Previous Class
class classA(Something):
CONFIG={
"opt_a": "opt_b"
}
def construct(self):
texts = self.texts
texts.append('Apple')
.
.
.
.
self.common_method()
# This is another class that inherits `MovingCameraScene`
class classB(MovingCameraScene):
CONFIG={
"opt_a": "opt_b"
}
def construct(self):
texts = []
texts.append('Apple')
.
.
.
.
// More code that was in `common_method` of `Something` but directly pasted here.
还有很多 class 但希望这应该足够了。
当我尝试使用自己的 class (Something
) 作为 classA
的父级时,出现以下错误:
'classA' object has no attribute 'camera'
我在使用直接继承自 MovingCameraScene
的 classB
时没有收到此错误。
你没有在Something
中调用超级构造函数:
class Something(MovingCameraScene):
CONFIG = {
"alpha": "beta"
}
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.texts = []