classA在Python中没有属性“camera”错误

2024-09-27 23:20:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个包含大量文件和类的大型Python库。我试图创建自己的类以防止代码重复。下面是所有课程的基本概述

# 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.

还有很多课程,但希望这足够了

当我尝试使用我自己的类(Something)作为classA的父类时,出现以下错误:

'classA' object has no attribute 'camera'

当使用直接从MovingCameraScene继承的classB时,我没有遇到这个错误


Tags: selfconfiginitdefscenemethodsomethingkwargs

热门问题