Mixins
A mixin is a class written to be inherited alongside a real base, providing a slice of behaviour and no independent identity. You instantiate the combined class, not the mixin.
Detailed Explanation
Inheritance can mean "is-a" or it can mean "please bolt these methods on". A mixin is the second meaning, made explicit. AuthMixin does not stand alone. class AdminView(AuthMixin, View): is a View that also knows how to check a user. The mixin is listed first so its methods sit earlier in the MRO and can call super() into the view.
A good mixin is small. It adds one capability: logging, JSON rendering, permission checks, retry. It should not carry the object's primary data. It should not assume a concrete sibling except through documented hook methods (self.get_user(), self.dispatch()). If the mixin needs a database, take it as a constructor argument on the concrete class, not as a hidden import inside the mixin.
Cooperative super() is required. The mixin calls super().method() so other mixins and the concrete class still run. Its __init__, if it has one, takes *args, **kwargs and forwards them. A mixin that reaches into OtherClass.method(self) by name is not a mixin; it is a hard dependency.
Name mixins with a Mixin suffix or an -able adjective (JSONRenderable) so readers do not treat them as types. isinstance(x, AuthMixin) is usually the wrong test; the type that matters is View.
Python's standard library uses the idea: socketserver.ThreadingMixIn, collections.abc helpers, test-case mixins in some frameworks. Django class-based views are a large mixin graph. That ecosystem works because the project documented the hook names. Your application probably needs two mixins, not twelve.
If you cannot explain the MRO without a diagram, use composition. self.auth = AuthService(...) is a mixin you can test and replace.
Code Examples
class JSONMixin:
def render(self) -> str:
import json
return json.dumps(self.to_dict(), sort_keys=True)
class Order:
def __init__(self, sku: str, qty: int):
self.sku = sku
self.qty = qty
def to_dict(self) -> dict:
return {"sku": self.sku, "qty": self.qty}
class JsonOrder(JSONMixin, Order):
pass
print(JsonOrder("A-1", 2).render()) # {"qty": 2, "sku": "A-1"}class TimingMixin:
def dispatch(self, event: str) -> str:
result = super().dispatch(event)
return f"timed[{result}]"
class Sink:
def dispatch(self, event: str) -> str:
return event
class TimedSink(TimingMixin, Sink):
pass
print(TimedSink().dispatch("pay")) # timed[pay]Common Gotchas
Instantiating a mixin
it is missing the methods it expects to find via `super()` or hooks. Instantiate the concrete combination.
Mixin order is part of the API
`class C(AMixin, BMixin, Base)` is not `class C(BMixin, AMixin, Base)`. Put mixins left of the concrete base.
State on a mixin is shared if it is a class attribute
`seen = []` on the mixin is one list for every subclass. Use instance attributes set in a cooperative `__init__`.
A mixin is still inheritance
`isinstance` and API coupling apply. Composition avoids both.