In Python, how to make data members visible to subclasses if not known when initializing an object?
<p>The title is a bit long, but it should be pretty straightforward for someone well-aware of python.</p>

<p>I'm a python newbie. So, maybe i'm doing things in the wrong way.</p>

<p>Suppose I have a class TreeNode</p>

<pre><code>class TreeNode(Node):
    def __init__(self, name, id):
        Node.__init__(self, name, id) 
        self.children = []
</code></pre>

<p>and a subclass with a weight:</p>

<pre><code>class WeightedNode(TreeNode):
    def __init__(self,name, id):
        TreeNode.__init__(self, name, id)
        self.weight = 0
</code></pre>

<p>So far, i think I'm ok. Now, I want to add an object variable called father in TreeNode so that WeightedNode has also this member. The problem is that I don't know when initializing the object who is going to be the father. I set the father afterwards with this method in TreeNode :</p>

<pre><code>def set_father(self, father_node):
    self.father = father_node 
</code></pre>

<p>The problem is then when i'm trying to access self.father in Weighted:</p>

<pre><code>print 'Name %s Father %s '%(self.name, self.father.name)
</code></pre>

<p>I obtain:</p>

<pre><code>AttributeError: WeightedNode instance has no attribute 'father'
</code></pre>

<p>I thought that I could make <code>father</code> visible by doing something in <code>TreeNode.__init__</code> but i wasn't able to find what.</p>

<p>How can i do that ?</p>

<p>Thanks.</p>
