I want to create functionality for a data class to be able to inherit 0..N mix-ins in Python whilst still returning the original data class object.
I have a preliminarily working solution, albeit very ugly and not very pythonic.
from typing import Any
from dataclasses import dataclass
@dataclass
class Customer:
salary = 0
monthlyBonus = 0
latestClassification = ""
class SalaryMixin:
def get_salary(self: Any) -> float:
return self.salary
class customerInformationMixin:
def get_latest_classification(self: Any) -> str:
return self.latestClassification
def create_customer(*mixins: Type) -> Customer:
customer = Customer()
for mixin in mixins:
mixin_methods = [func for func in dir(mixin) if callable(getattr(mixin, func)) and not func.startswith("__")]
for method in mixin_methods:
setattr(customer, method, getattr(mixin(), method))
return customer
Ultimately I would like a function create_customer()
that will accept any number of mix-ins and inherit its functionality.