Mixin In Python

Mixin is a concept in OOP programming language where class implements a set of methods which can then be reused by child class. However, it does not mean that Mixin class is a parent class.

Mixin can seem like inheritance, but overall if we see mixin as a class with a bunch of methods that can be used by its child class.

For example, there can be a ExportMixin which provides method to export class a dictionary, json and other formats.

class ExportMixin:
    def to_dict(self):
        # logic
    def to_josn(self):
        # logic to json
    def to_xml(self):
        # logic to xml
 
 
class A(ExportMixin):
    def __init__(self):
        self.value1 = 1
        self.value2 = 2
 
a = A()
a.to_dict()
a.to_json()
a.to_xml()

Open ended question

Is class without __init__ a mixin?