How would a parent class call its own method within itself in Python
<p>I am trying to create the concrete class <code>Val15</code> class with the Mixins, <code>MyCollection</code>, <code>Val5</code>, <code>Val10</code>. <code>Val15</code> is trying to inherit <code>values</code> from <code>MyCollection</code>. When <code>Val15</code> is instantiated, I am trying to call both <code>Val10</code>'s and <code>Val5</code>'s  <code>value</code> and add them to <code>Val15</code>, <code>values</code>. </p>

<p>However, it turns out that <code>Val15</code> will inherit only <code>Val10</code>'s value and all the classes will call <code>Val10</code>'s value. How exactly can you overturn this problem?</p>

<pre><code>class MyCollection(object):
    def __init__(self):
        super().__init__()
        self.values = []


class MyMessage(object):
    def __init__(self):
        super().__init__()

    def message(self):
        print('Hello World')


class Val10(object):
    def __init__(self):
        super().__init__()
        print('Call 10')
        self.values.append(self.value())

    def value(self):
        print('Value 10')
        return 10


class Val5(object):
    def __init__(self):
        super().__init__()
        print('Call 5')
        self.values.append(self.value())

    def value(self):
        print('Value 5')
        return 5


class Val15(Val10, Val5, MyMessage, MyCollection):
    def __init__(self):
        super().__init__()

    def my_sum(self):
        return sum(self.values)


val15 = Val15()
val15.my_sum()
val15.message()

# Result:
# Call 5
# Value 10
# Call 10
# Value 10
# Hello World
</code></pre>

<p>This is the interesting part:</p>

<pre><code>class Val10(object):
    def value(self):
        return 10

class Val5(object):
    def value(self):
        return 5
</code></pre>

<p>To get the sum of parents' values, you have to call each of them:</p>

<pre><code>class Val15(Val10, Val5):
    def value(self):
        return Val10.value(self) + Val5.value(self)
</code></pre>

<h2>Alternative solution</h2>

<p>Another approach is to define that <code>value</code> always returns the sum of all inherited values, whatever they may be, without knowing which parent classes' <code>value</code> to call:</p>

<pre><code>class Val10:
    def value(self):
        previous = getattr(super(), 'value', lambda: 0)()
        return previous + 10

class Val5:
    def value(self):
        previous = getattr(super(), 'value', lambda: 0)()
        return previous + 5

class Val15(Val10, Val5):
    def value(self):
        return getattr(super(), 'value', lambda: 0)()

Val15().value()  # 15
</code></pre>

<p>This solution does not make any assumptions about the way inheritance is going to be configured.</p>

<p><strong>Remarks:</strong></p>

<ul>
<li>The answer assumes that this is Python 3, because of the <code>super()</code> syntax</li>
<li><code>getattr(super(), 'value', lambda: 0)</code> returns the bound <code>value</code> method of the parent class, if such exists, or defaults to a function which returns 0.</li>
</ul>

python inheritance mixins