Search code examples
pythonpython-3.xoopinheritancemanim

classA has no attribute `camera` error in Python


I am working with a large Python library with a lot of files and Classes. I tried to create my own class to prevent code duplication. Here is a basic overview of all the 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.

There are many more classes but hopefully this should be enough.

I get the following error when I try to use my own class (Something) as parent of classA:

'classA' object has no attribute 'camera'

I don't get this error when using classB which directly inherits from MovingCameraScene.


Solution

  • You do not call the super constructor in Something:

    class Something(MovingCameraScene):
          
          CONFIG = {
              "alpha": "beta"
          }
    
          def __init__(self, **kwargs):
               super().__init__(**kwargs)
               self.texts = []