Multiple inheritance
Multiple inheritance is a class that lists more than one base: class View(AuthMixin, TemplateView):. Python flattens the bases into a single method resolution order so each attribute has one winner.
Detailed Explanation
Java allows one superclass and many interfaces. Python allows many superclasses. That is powerful and easy to make unreadable. The language feature is real; the design pattern you want is usually a mixin: a small class that provides methods and is not meant to be instantiated alone.
When a name exists on more than one base, the MRO decides who wins. The MRO is a C3 linearisation of the class and its ancestors. You can print it with Class.__mro__ or Class.mro(). The first class in the list that defines the name is used. super() continues after the current class, so a cooperative mixin calls super().dispatch() and the next mixin or the concrete class runs.
Cooperation is the contract. Every class in the diamond should take *args, **kwargs in __init__ and call super().__init__(*args, **kwargs) until object. A mixin that calls a concrete parent by name, or that does not call super(), breaks the chain. That is why people say "use mixins or use one base, do not invent a diamond of concrete classes".
Attribute lookup is not merge. Methods are not both called unless someone calls super. Fields are not concatenated. If AuthMixin and TemplateView both define dispatch, one wins. Read the MRO when the wrong method runs.
Prefer composition when the two behaviours do not form a type. A class that is both a HttpHandler and a Database is usually a handler that has a database. Multiple inheritance is for "this view is a template view and it is authenticated", where both bases are in the same family of objects.
Python also uses multiple inheritance for ABCs and protocols (class MyList(list, SomeABC):). That is structural, not a pile of business logic.
Code Examples
class LoggerMixin:
def dispatch(self, event: str) -> str:
return f"log:{super().dispatch(event)}"
class Handler:
def dispatch(self, event: str) -> str:
return f"handle:{event}"
class LoggedHandler(LoggerMixin, Handler):
pass
print(LoggedHandler().dispatch("pay")) # log:handle:pay
print(LoggedHandler.__mro__)
# (LoggedHandler, LoggerMixin, Handler, object)# Cooperative __init__: each class calls super so the whole chain runs.
class A:
def __init__(self, **kwargs):
self.a = kwargs.pop("a")
super().__init__(**kwargs)
class B:
def __init__(self, **kwargs):
self.b = kwargs.pop("b")
super().__init__(**kwargs)
class C(A, B):
def __init__(self, **kwargs):
super().__init__(**kwargs)
c = C(a=1, b=2)
print(c.a, c.b) # 1 2Common Gotchas
A mixin that does not call `super()` swallows the rest of the chain
later bases never see `__init__` or `dispatch`.
Base order matters
`class C(A, B)` prefers `A`. Flip the list and a different method wins. Put mixins left of the concrete base.
Diamonds of concrete classes are hard to debug
two "real" parents that both own state fight. Keep extra bases as mixins.
`super(Class, self).method()` is the old form
in Python 3, bare `super().method()` is enough inside the class body.