I'm working on some code that seems like will be a good fit for multiple inheritance, and I'm reading about multiple inheritance and super() to make sure I correctly understand how they work.
My understanding of super() is that (paraphrasing) it walks a class's MRO and returns a "dispatcher" of sorts, where accessing a method or attribute via __getattr__ accesses the "first" implementation of that method or attribute, as dictated by the MRO.
However, this example from RealPython has me absolutely baffled. (I've edited their example to remove all the functional methods and add print statements, since all I care about here is how super() works with the MRO and the various __init__ methods.)
class Rectangle:
def __init__(self, length, width, **kwargs):
print("Running Rectangle.__init__")
self.length = length
self.width = width
super().__init__(**kwargs)
class Square(Rectangle):
def __init__(self, length, **kwargs):
print("Running Square.__init__")
super().__init__(length=length, width=length, **kwargs)
class Triangle:
def __init__(self, base, height, **kwargs):
print("Running Triangle.__init__")
self.base = base
self.height = height
super().__init__(**kwargs)
class RightPyramid(Square, Triangle):
def __init__(self, base, slant_height, **kwargs):
print("Running RightPyramid.__init__")
self.base = base
self.slant_height = slant_height
kwargs["height"] = slant_height
kwargs["length"] = base
super().__init__(base=base, **kwargs)
_ = RightPyramid(10, 5)
.