Is there a way to include the same Mixin more than once in a subclass in Python?
<p>Normally a Python mixin would only be included once, like:</p>

<pre><code>class MyMixin:
    pass
class Child(Base, MyMixin):
    pass
</code></pre>

<p>However, in some situation it would be handy if we can use the same Mixin twice. 
For example, I have a Mixin in SQLAlchemy defining some Columns as follow:</p>

<pre><code>class MyNoteMixin:
    note = Column(String)
    by_who = Column(String)
</code></pre>

<p>Now I have a subclass that inherits from the above Mixin, but needs two different columns both are of a note nature. Can I do something like:</p>

<pre><code>class Child(Base, MyNoteMixin as "Description", MyNoteMixin as "Quote"):
        pass
</code></pre>

<p>So that the resolved table would contain a column named Description that is exactly a MyNoteMixin copy except for the name, AND also has another column named Quote with the same nature.</p>

<p>Is this possible with SQLAlchemy? Or generally speaking, is such usage of Mixin even possible in Python? Thanks.</p>

<p>I would recommend using <a href="https://docs.python.org/2/glossary.html#term-decorator" rel="nofollow">python decorator</a>. </p>

<p>Here is an example how to get this using plan python class:</p>

<pre><code>def custom_fields(**kwargs):
    def wrap(original_class):
        """
        Apply here your logic, could be anything!
        """
        for key, val in kwargs.items():
            setattr(original_class, key, val)
        return original_class
    return wrap


@custom_fields(quote='String here, can be SQLAlchemy column object')
class Child:
    pass


print(Child.quote)
</code></pre>

<p>Output:</p>

<pre><code>&gt;&gt;&gt; String here, can be SQLAlchemy column object
</code></pre>

<p>You will have to adapt it for sqlalchemy, like connect the <code>setattr</code> to your <code>MyNoteMixin</code>.</p>

python sqlalchemy mixins