Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My first suggestion is to change the name of your class with a more clear one following the <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP-8</a>. It was a bit hard to manage a class name such as <code>t</code>:</p> <pre><code>class Tree: def __init__(self, i): self.l = [] self.a = 0 for ii in range(i): self.a = ii self.l.append(Tree(i-1)) </code></pre> <p>Now, you should change the <code>__iter__()</code> method to return the next element in <code>self</code>, not <code>self</code> itself - no pun intended :) The <code>__iter__()</code> method should return an iterator to the original object, not the object itself:</p> <pre><code>def __iter__(self): return next(self) </code></pre> <p>Now comes the hard part: the <code>next()</code> method. I always find it hard to write a recursive iterators but this is not that impossible: for each child, iterate over it and yield the iterated value: </p> <pre><code>def next(self): for i in self.l: for ii in i: yield ii yield self </code></pre> <p>Since the method is recursive, it takes care of yielding all descendants. When the <code>next()</code> method is called at a leaf node (a node without children) it will just return the node itself. OTOH, when called on a node with children, it will call itself for each children and yield the returned value. This means that it will be called by the children of the children and so on until the leaf nodes. After being called by <em>all</em> descendants of a node - which means that all descendants were yielded -, it should yield its own value, so you have to yield the original node itself.</p> <p>Now your <code>printall()</code> function should work flawlessly:</p> <pre><code>if __name__ == "__main__": t = Tree(6) t.printall() </code></pre> <p>Some final considerations:</p> <ul> <li><p>Always make your classes extend the <code>object</code>:</p> <p>class Tree(object)::</p></li> <li><p>I bet you want to write a <code>__init__()</code> method like the one below:</p> <pre><code>def __init__(self, i): self.l = [] self.a = i for ii in range(i): self.l.append(Tree(i-1)) </code></pre></li> <li><p>The wberry solution is better because it is more concise and, probably, more efficient. However, I think the OP is studying trees, recursion etc. so I thought a more hardcoded solution would be instructive :)</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload