0

In the following code I instantiate RightPyramid as:

      p = RightPyramid(base=2, slant_height=7)
      p.area()
 class Rectangle:
     def __init__(self, length, width, **kwargs):
        self.length = length
        self.width = width
        super().__init__(**kwargs)

     def area(self):
        return self.length * self.width

     def perimeter(self):
        return 2 * self.length + 2 * self.width


 class Square(Rectangle):
     def __init__(self, length, **kwargs):
        super().__init__(length=length, width=length, **kwargs)
     

 class Triangle:
     def __init__(self, base, height):
        self.base = base
        self.height = height
        super().__init__()

     def tri_area(self):
        return 0.5 * self.base * self.height
 
 
 
 class RightPyramid(Square, Triangle):
     def __init__(self, base, slant_height, **kwargs):  
        self.base = base
        self.slant_height = slant_height
        kwargs["height"] = slant_height 
        kwargs["length"] = base
        super().__init__(base=base, **kwargs)
     
         
     def area(self):
        base_area = super().area()
        perimeter = super().perimeter()
        return 0.5 * perimeter * self.slant_height + base_area

     def area_2(self):
        base_area = super().area()
        triangle_area = super().tri_area()
        return triangle_area * 4 + base_area


The RightPyramid _mro_ results in:

(__main__.RightPyramid,
__main__.Square,
__main__.Rectangle,
__main__.Triangle,
object)

My question is:

What is super()._init_(**kwargs) in Rectangle class calling? I think it calls the init from Triangle class, but Rectangle does not inherit from Triangle class; on the other hand it precedes Triangle in MRO

Ali
  • 13
  • 2
  • @Ali, Rectangle.__init__ called from super() in Square.__init__ – Andrey Jan 23 '22 at 02:18
  • *"What is `super().__init__(**kwargs)` in `Rectangle` class calling?"* It depends on the context. If you create an instance of the `Rectangle` class then `super().__init__` is `object.__init__` since `object` is next after `Rectangle` in `Rectangle.__mro__`. If you create an instance of the `RightPyramid` class then `super().__init__` in the `Rectangle` class is `Triangle.__init__` since `Triangle` is next after `Rectangle` in `RightPyramid.__mro__`. – kaya3 Jan 23 '22 at 02:24

0 Answers0