Dynamically add class variables to classes inheriting mixin class
<p>I've got a mixin class, that adds some functionality to inheriting classes, but the mixin requires some class attributes to be present, for simplicity let's say only one property <code>handlers</code>. So this would be the usage of the mixin:</p>

<pre><code>class Mixin:
    pass

class Something(Mixin):
    handlers = {}
</code></pre>

<p>The mixin can't function without this being defined, but I really don't want to specify the <code>handlers</code> in every class that I want to use the mixin with. So I solved this by writing a metaclass:</p>

<pre><code>class MixinMeta:
    def __new__(mcs, *args, **kwargs):
        cls = super().__new__(mcs, *args, **kwargs)
        cls.handlers = {}
        return cls

class Mixin(metaclass=MixinMeta):
    pass
</code></pre>

<p>And this works exactly how I want it to. But I'm thinking this can become a huge problem, since metaclasses don't work well together (I read various metaclass conflicts can only be solved by creating a new metaclass that resolves those conflicts).</p>

<p>Also, I don't want to make the <code>handlers</code> property a property of the <code>Mixin</code> class itself, since that would mean having to store handlers by their class names inside the <code>Mixin</code> class, complicating the code a bit. I like having each class having their handlers on their own class - it makes working with them simpler, but clearly this has drawbacks.</p>

<p>My question is, what would be a better way to implement this? I'm fairly new to metaclasses, but they seem to solve this problem well. But metaclass conflicts are clearly a huge issue when dealing with complex hierarchies without having to define various metaclasses just to resolve those conflicts.</p>

<p>Your problem is very real, and Python folks have thought of this for Python 3.6 (still unrealsed) on. For now (up to Python 3.5), if your attributes can wait to exist until your classes are first instantiated, you could put cod to create a (class) attribute on the <code>__new__</code> method of your mixin class itself - thus avoiding the (extra) metaclass:</p>

<pre><code>class Mixin:
    def __new__(cls):
         if not hasattr(cls, handlers):
              cls.handlers = {}
         return super().__new__(cls)
</code></pre>

<p>For Python 3.6 on, <a href="https://www.python.org/dev/peps/pep-0487/" rel="nofollow">PEP 487</a> defines a <code>__init_subclass__</code> special method to go on the mixin class body. This special method is not called for the mixin class itself, but will be called at the end of <code>type.__new__</code> method (the "root" metaclass) for each class that inherits from your mixin.</p>

<pre><code>class Mixin:
    def __init_subclass__(cls, **kwargs):
         cls.handlers = {}
         return super().__init_subclass__(**kwargs) 
</code></pre>

<p>As per the PEP's background text, the main motivation for this is exactly what led you to ask your question: avoid the need for meta-classes when simple customization of class creation is needed, in order to reduce the chances of needing different metaclasses in a project, and thus triggering a situation of metaclass conflict.</p>

python python-3.x