Skip part of superclass __init__?
<p>I have a class which subclass another. I would like to skip a part of the initialization process, for example:</p>

<pre><code>class Parent:
    __init__(self, a, b, c, ...):
        # part I want to keep:
        self.a = a
        self.b = b
        self.c = c
        ...
        # part I want to skip, which is memory and time consuming
        # but unnecessary for the subclass:
        self.Q = AnotherClass()

class Child(Parent):
    __init__(self):
        #a part of the parent initialization process, then other stuff
</code></pre>

<p>The two solutions I've come up with are:</p>

<ul>
<li>Make an abstract class parent of the <code>Parent</code> class which wouldn't include the unwanted part of the initialization for the <code>Child</code>, or</li>
<li>Duplicate just the part of the initialization of the parent that I want in the child</li>
</ul>

<p>Which is best, or are there better methods?</p>

<p>What about wrapping creation of <code>Q</code> into private method, and overriding that method in subclass?</p>

<pre><code>class Parent:
    def __init__(self, a, b, c, ...):
        # part I want to keep:
        self.a = a
        self.b = b
        self.c = c
        self._init_q()

    def _init_q():
        self.Q = AnotherClass()

class Child(Parent):
    def _init_q(self):
        pass  # do not init q when creating subclass
</code></pre>

<p>It's not a cleanest approach, since if <code>Child</code> does not need <code>Q</code> either it shouldn't be a child of Parent, or maybe <code>AnotherClass</code> is misplaced (maybe it should be injected to methods which needs it) but it solves your issue without changing any of class interfaces.</p>

python inheritance