How to iterate through instances in a tree?
<p>I have a problem with a self-written tree class in python:</p>

<pre><code>class Tree:
    def __init__(self, parent=0, value=0):
        self.value = value
        self.parent = parent
    def __iter__(self): return self
    def next(self):
        tmp = self.value
        try:
            self.parent = self.parent.parent
            self.value = self.parent.value
        except AttributeError:
            raise StopIteration
        return tmp
    def sum(self):
        list_ = [item for item in self]
        print list_
        return sum(list_)
</code></pre>

<p>Actually, the "tree" is not fully written, but the current problem blocks further progress.
The structure has only two instance variables (<code>value</code>, <code>parent</code>).
I would like to sum values from the current instance to the first parent with iterators (if it is all together possible). The <code>sum</code> method is used for that (additional <code>list_</code> variable is unnecessary, but helps further to explain the problem). </p>

<p>When running a test case</p>

<pre><code>parent = Tree()
child = Tree(parent=parent, value=8)
child2 = Tree(parent=child,value=10)
print child2.sum()
</code></pre>

<p>I obtain the following:</p>

<pre><code>[10]
10
</code></pre>

<p>Please, could anybody explain why the list of values contains only one number though it should look like <code>[10,8]</code>? Seems the problem is in the implementation of <code><strong>iter</strong></code> and <code>next</code>, but I can't understand how to repair the solution.</p>

<p>Thank you in advance.</p>

<p>I'm not sure you can call this a <code>Tree</code>. One would expect parent node(s) and   multiple leaf nodes, and not just a linear connection of objects.</p>

<p>See: <a href="http://stackoverflow.com/questions/2482602/a-general-tree-implementation-in-python">a general tree implementation in python</a></p>

<p>On another note, if you want to implement a linkedlist, suggestions made in the comment to your question by Barny should be considered and as well, you can give an eye to: <a href="http://stackoverflow.com/questions/280243/python-linked-list">Python Linked List</a></p>

<p>Coming to your current implementation, you'll need some sort of loop, to walk from the current child node up until the head parent node. And when the next parent attribute is not found, stop the iteration. The following puts the logic in the <code>__iter__</code> method of the class, which is now a generator function:</p>

<pre><code>class Tree:
    def __init__(self, parent=None, value=0):
        self.value = value
        self.parent = parent

    def __iter__(self): 
        _parent = self.parent
        yield self.value
        while True:
            try:               
                yield _parent.value 
                _parent = _parent.parent
            except AttributeError:
                break  

    def sum_from_node(self):
        list_ = [item for item in self]
        print list_
        return sum(list_)
</code></pre>

<p><em>Demo:</em></p>

<pre><code>parent = Tree()
child = Tree(parent=parent, value=8)
child2 = Tree(parent=child,value=10)
child3 = Tree(parent=child2,value=4)
print child3.sum_from_node()
# [4, 10, 8, 0]
# 22
</code></pre>

python python-2.7