Glossary

Mixin

A mixin is a class that provides a specific piece of functionality intended to be "mixed in" to other classes via multiple inheritance. Mixins are not meant to stand alone — they add capabilities (like serialisation, logging, or comparison) without being a complete class. By convention, mixin class names end with Mixin (e.g., SerializableMixin, LoggingMixin).

The mixin pattern works well in Python because of multiple inheritance and the well-defined Method Resolution Order (MRO). A common example is a JSONMixin that adds a .to_json() method to any class, or a ComparableMixin that implements all six comparison operators from just __eq__ and __lt__. The standard library's socketserver.ThreadingMixIn and socketserver.ForkingMixIn add concurrency strategies to server classes.

Mixins should be narrow in scope — each mixin provides one cohesive piece of functionality. They should not have __init__ methods that require arguments (or if they do, they must call super().__init__(**kwargs) cooperatively). The mixin pattern is an alternative to deep inheritance hierarchies: instead of a tall class tree, you compose a flat set of mixins. In modern Python, Protocols and composition are often preferred over mixins, but the pattern remains valuable when you need to inject behaviour into multiple unrelated class hierarchies.

Related terms: Inheritance, Composition, Method Resolution Order

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.