super() in multiple inheritance?class A: def greet(self): return "Hello from A" class B(A): def greet(self): return super().greet() + " and B" class C(A): def greet(self): return super().greet() + " and C" class D(B, C): def greet(self): return super().greet() + " and D" obj = D() print(obj.greet())
super() follows the method resolution order (MRO) in multiple inheritance.The method resolution order for class D is D → B → C → A. Each greet method calls super().greet(), so the calls chain through A, then C, then B, then D, concatenating strings as "Hello from A and B and C and D".
super() with arguments?class Base: def __init__(self, x): self.x = x class Child(Base): def __init__(self, x, y): super(Child, self).__init__(x) self.y = y obj = Child(5, 10) print(obj.x, obj.y)
super() is called with explicit arguments and how the base class constructor is called.The Child class calls super(Child, self).__init__(x), which correctly calls the Base constructor with x. So obj.x is 5 and obj.y is 10.
super()?class Parent: def action(self): return "Parent action" class Child(Parent): def action(self): return "Child action" obj = Child() print(obj.action())
super(), the base method is not executed.The Child class overrides action and returns its own string without calling super(). So the output is "Child action".
super() in a diamond inheritance pattern?class Top: def process(self): return "Top" class Left(Top): def process(self): return super().process() + "->Left" class Right(Top): def process(self): return super().process() + "->Right" class Bottom(Left, Right): def process(self): return super().process() + "->Bottom" obj = Bottom() print(obj.process())
super() calls chain.The MRO for Bottom is Bottom → Left → Right → Top. Each process calls super().process(), so the string builds as "Top->Left->Right->Bottom".
super() instead of directly calling a parent class method in Python?super() respects the method resolution order (MRO), which is essential for multiple inheritance to work correctly. Direct calls to parent classes can skip other classes in the MRO, causing bugs.