Better Programming: Mixins in Python

python software design patterns software development Aug 02, 2023

Sometimes there are different types of functionality we want to include in multiple classes that are not related to each other. For example, when designing a user interface, we may create geometric shapes (circle, rectangle, etc) or widgets (buttons, pop-ups, etc). These are two completely different object hierarchies, yet both these families and others may need to share certain functionality, like being rendered on the screen.

The not-so-smart way to solve this is to join them all into a common object hierarchy and implement that render() method in the base class. However, there is a better way to do it: Mixins.

Mixins are classes designed to provide specific functionality to other classes that need it. They are designed so other classes can inherit from them without running into typical multiple inheritance problems (ambiguous methods, unnecessary inherited functionality, etc).

To accomplish this, mixins follow some rules. For starters, they only contain methods, not attributes, because mixins only provide functionality. Also, they tend to be simple, with a few closely related methods. It’s also important to note that mixins should not belong to a class hierarchy, but rather be an independent class.

Let’s look at a simple example. Imagine we have a robot, which we want to give certain capabilities to. Different robots will have different skills, so we can encapsulate each of those skills in a mixin:

Then, we can compose the skills that our robot needs by inheriting from the necessary mixins:

By inheriting from the appropriate mixins, we can make our class adopt the functionality implemented in them. Since mixins are standalone, simple, stateless classes, we are not likely to have clashes by inheriting from several of them at the same time.

This mechanism has made our robots flexible (they can have a varied set of skills) and simpler (no need to create complicated class structures or implement the functionality on every class that needs it.

Let's see it with another, real world example. Suppose that we're working on a data management application and in it we have a class called DataTable, that holds and allows us to inspect certain data. We want to add functionality to this class to be able to export its data in csv and json format, as well as log the operations performed by objects of this class to the console. Luckily, mixins allow us to do this easily:


When to use Mixins

In summary, mixins are a great tool when:

  • You need to reuse functionality across multiple classes that are not directly related via inheritance.
  • You need to compose behavior from multiple sources into a class.
  • You want segregated interfaces for the functionality that you want to mix-in.



AI moves fast. We help you keep up with it.

Get a monthly selection of the most groundbreaking advances in the world of AI, top code repositories, and our best articles and tutorials. 

We hate SPAM. We will never sell your information, for any reason.