Abstract base classes and mix-ins in python

In the python docs, I read this about the ABC (abstract base class) meta class: Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. I don't come from a strong technical background so can someone explain this to me in really simple terms? I have an ABC, which represents a set of methods to ingest, parse, transform and write the data contained in different types of file (e.g. csv, docx, pdf, json, xml etc.) and I have lots of subclasses of that which concretely implement the ABC's abstract methods. However, there are a bunch of helper / private methods that I want to use across these different classes, and rather than repeat them across all the class definitions, I thought it would make sense to have them in their own class (or set of classes). Doing a bit of reading I came across the term mix-in and that seemed like a good description of what I'm trying to do. Initially, I had a separate mix-in class which was essentially just a collection of useful methods, and the concrete implementation of my file processor classes inherited from both the base class and the mix-in class. I'm really confused as to why I would want the ABC subclassed by the mix-in directly as stated in the docs - would this mean my concrete classes would no longer need to inherit from the base class (as they'd have that as their grandparent class)? This seems to obscure the logic of my program and moves away from what I thought mix-ins were - i.e. not designed to be instantiated or inherited from directly, but more just a collection of methods that can be added to other classes through multiple inheritance. So I guess my questions are - what does good practice in this situation look like? Should my mix-in inherit from the ABC or not? Should my concrete classes then only inherit from the mixin, or from both the mixin and ABC? If it's up to me either way, what do you gain or lose from each approach? Is this even the correct way to use a mixin?

Apr 23, 2025 - 11:05
 0
Abstract base classes and mix-ins in python

In the python docs, I read this about the ABC (abstract base class) meta class:

Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class.

I don't come from a strong technical background so can someone explain this to me in really simple terms?

I have an ABC, which represents a set of methods to ingest, parse, transform and write the data contained in different types of file (e.g. csv, docx, pdf, json, xml etc.) and I have lots of subclasses of that which concretely implement the ABC's abstract methods.

However, there are a bunch of helper / private methods that I want to use across these different classes, and rather than repeat them across all the class definitions, I thought it would make sense to have them in their own class (or set of classes). Doing a bit of reading I came across the term mix-in and that seemed like a good description of what I'm trying to do.

Initially, I had a separate mix-in class which was essentially just a collection of useful methods, and the concrete implementation of my file processor classes inherited from both the base class and the mix-in class.

I'm really confused as to why I would want the ABC subclassed by the mix-in directly as stated in the docs - would this mean my concrete classes would no longer need to inherit from the base class (as they'd have that as their grandparent class)? This seems to obscure the logic of my program and moves away from what I thought mix-ins were - i.e. not designed to be instantiated or inherited from directly, but more just a collection of methods that can be added to other classes through multiple inheritance.

So I guess my questions are - what does good practice in this situation look like? Should my mix-in inherit from the ABC or not? Should my concrete classes then only inherit from the mixin, or from both the mixin and ABC? If it's up to me either way, what do you gain or lose from each approach? Is this even the correct way to use a mixin?