Call Grandparent Method without executing parent method through Mixin
<p>I need to override Parent method and call Grandparent method through mixin. Is it possible? </p>

<p>For example: <code>A</code> and <code>B</code> are library classes.</p>

<pre><code>class A(object):
    def class_name(self):
        print "A"


class B(A):
    def class_name(self):
        print "B"
        super(B, self).class_name()
    # other methods ...
</code></pre>

<p>Now I need to override <code>class_name</code> method from <code>B</code> and call it's super.</p>

<pre><code>class Mixin(object):
    def class_name(self):
        print "Mixin"
        # need to call Grandparent class_name instead of parent's
        # super(Mixin, self).class_name()


class D(Mixin, B):
    # Here I need to override class_name method from B and call B's super i.e. A's class_name, 
    # It is better if I can able to do this thourgh Mixin class. (
    pass
</code></pre>

<p>Now when I called <code>D().class_name()</code>, it should print <code>"Mixin" and "A"</code>only. Not "B"</p>

<p>One method is to use <a href="https://docs.python.org/2/library/inspect.html#inspect.getmro" rel="nofollow"><code>inspect.getmro()</code></a> but that is likely to break if the user writes <code>class D(B, Mixin)</code>.</p>

<p>Let me demonstrate:</p>

<pre><code>class A(object):
    def class_name(self):
        print "A"


class B(A):
    def class_name(self):
        print "B"
        super(B, self).class_name()
    # other methods ...

class Mixin(object):
    def class_name(self):
        print "Mixin"
        # need to call Grandparent class_name instead of parent's
        # super(Mixin, self).class_name()


class D(Mixin, B):
    # Here I need to override class_name method from B and call B's super i.e. A's class_name, 
    # It is better if I can able to do this thourgh Mixin class. (
    pass

class E(B, Mixin): pass
</code></pre>

<p><br /></p>

<pre><code>import inspect
print inspect.getmro(D) # returns tuple with (D, Mixin, B, A, object)
print inspect.getmro(E) # returns tuple with (E, B, A, Mixin, object)
</code></pre>

<p>So if you have control and can ensure that you always get <code>Mixin</code> first. You can use <code>getmro()</code> to get the grandparent and execute it's <code>class_name</code> function.</p>

python multiple-inheritance