QamoosTech
Programming FundamentalsObject-oriented programmingIntermediatePythonDjango

Mixin

PronunciationMIK-sin

Definition

A mixin is a small class that adds one specific behaviour to other classes through inheritance. It is not meant to be used on its own.

Where you hear it

In Python and Django code, for example mixins that add logging or JSON output, and in design discussions about reuse.

Examples

  • JsonMixin gives any class a to_json() method.
  • The view inherits from the login mixin to require authentication.
  • The Timestamps mixin adds created_at and updated_at to any model that inherits it.

Common mistake

Piling up many mixins with overlapping behaviour. The lookup order gets confusing; keep each mixin focused.

Don't confuse with

A normal base class that represents what something is. A mixin only adds a capability.

Say it at work

  • In a meeting or chatPut that logging in a mixin and reuse it across the views.
  • In an email or ticketKeep the mixin small: one behaviour only.