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
Pythonimport 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}Explore class relationships in the OOP Playground