Skip to main content

Python Multiple Inheritance & Mixins

OOP Architecture

Multiple Inheritance & Mixins

Python allows a class to inherit from multiple bases. Learn when to embrace it and how to avoid the dreaded diamond problem.

Mixins

  • Lightweight classes that provide reusable methods but no state.
  • Naming convention: SomethingMixin.
  • Example:
class TimestampMixin:
created_at: datetime

def touch(self):
self.created_at = datetime.utcnow()

class AuditedModel(TimestampMixin, BaseModel):
...

Best practices

  • Keep mixins focused on a single behavior.
  • Ensure cooperative super() calls so each parent runs.
  • Document expected attributes/methods that mixins rely on.

Next up in your learning path