Skip to content

Instantly share code, notes, and snippets.

@luchiago
Created July 9, 2020 23:10
Show Gist options
  • Save luchiago/1667b50f44a824f088b992d76781e481 to your computer and use it in GitHub Desktop.
Save luchiago/1667b50f44a824f088b992d76781e481 to your computer and use it in GitHub Desktop.
[BACKEND][PYTHON][DJANGO] What are mixins?

Mixins

Reference

Motivation to use Mixins

A way to achieve modularity and reusability in OO is through this concept. Multiple languages implement this in different ways. In Python, mixins are supported via multiple inheritance

Context: Python

Mixin is a parent class that provides functionality to subclasses but is not intended to be instantiated itself. It can have both and concrete and abstract methods that are basically abstract base classes. Mixins can be regarded as a specific strain of abstract base classes where they can house both concrete and abstract methods but don't keep any internal states. These can help you when

  • You want to provide a lot of optional features for a class.
  • You want to provide a lot of not-optional features for a class, but you want the features in separate classes so that each of them is about one feature (behavior).
  • You want to use one particular feature in many different classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment