Browse OOP reference
Types and contracts

What are mixins?

On this page

A mixin contributes a focused behaviour to another class, often relying on a small set of methods supplied by its host. A Python JSON-output mixin might call self.to_mapping(). Document that requirement and avoid silently adding state with names that collide with the host.

Worked example

Python
import json

class JsonMixin:
    def as_json(self):
        return json.dumps(self.to_mapping())

class Release(JsonMixin):
    def to_mapping(self):
        return {"release": 8}

assert json.loads(Release().as_json()) == {"release": 8}

Python tutorial on classes

Explore class relationships in the OOP Playground

What is a method?What is multiple inheritance?What is a class?

All OOP questions

Search ObjectFAQ