referencing class members from child class
<p>Edit: sorry for the confusion about parent/child.  I am new to OOP so confusing my terms.  </p>

<p>I would like to understand the appropriate strategy to reference class attributes which will be provided by an inherited (child class).  </p>

<p>I have a case where I have a common base class which will be used, and multiple parent classes which the child class can inherit from.  Each child class will have the same set of methods but different implementations.  The problem I have is that I need the child class to reference class attributes of the parent.  The code below seems to work fine but my IDE (pycharm) complains of an unresolved attribute reference, so I am wondering if this is an anti-pattern.  </p>

<p>Module <strong>parent1.py</strong>:</p>

<pre><code>class Parent(object):
    def __init__(self):
       pass
    def do_something(self):
       result = self.instance_id
</code></pre>

<p>Module <strong>parent2.py</strong>: </p>

<pre><code>class Parent(object):
    def __init__(self):
        pass
    def do_something(self):
        result = self.instance_id+1
</code></pre>

<p>Module <strong>child.py</strong>:</p>

<pre><code>if CONSTANT:
    from parent1 import Parent
else:
    from parent2 import Parent

class Child(Parent):
    def __init__(self, instance_id):
         self.instance_id = instance_id
         Parent.__init__(self)
</code></pre>

<p>In this case the pycharm IDE flags the reference to self.instance_id in Parent1 and Parent2 as an 'Unresolved attribute reference' even though the code works.  I thought about moving this into each parent class, but nothing about instance_id needs to change in the Parent class, so it feels redundant to define it once for each Parent.  So my question is as follows:
1.  Is there anything wrong with this code organization?
2.  If there is nothing wrong with it, how can i fix the IDE to resolve this reference?</p>

<p>Use a <code>BaseParent</code> class that defines <code>instance_id</code>, and have both <code>Parent</code> implementations inherit from it. This will shut up your IDE. As a side benefit, <code>Child</code> can extend either <code>Parent</code>, but will always be an instance of <code>BaseParent</code>.</p>

<p>That aside, this is not very <em>kosher</em> object-oriented design. Changing the class hierarchy during program execution falls under <em>metaprogramming</em>, a very powerful tool that should be rarely used.</p>

<p>Metaprogramming, like multiple inheritance, is sometimes the perfect solution for a problem -- but it comes at a cost. Both introduce a layer of complexity that's hard to understand, easy to miss while walking the codebase and difficult to maintain afterwards.</p>

<p>This is especially true when the reader is not the implementer, or even for the same person after some time. I would revise the decision, in most cases this kind of approach is not necessary.</p>

python class python-3.x