Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the code you have posted, it's clear that what you are missing is <em>what a generator does</em>, and <em>how <code>__iter__</code> and <code>next</code> are supposed to behave</em></p> <p>So let's start with the iterator protocol. an object is iterable if it returns an iterator when its <code>__iter__</code> method is called, and an iterator is an object that has a <code>next</code> method, which can be called zero or more times and should eventually raise <code>StopIteration</code>. </p> <p>It's not to unusual for certain kinds of objects to be their own iterators (that have <code>__iter__</code> return <code>self</code>), but this is usually limited to objects that somehow themselves represent a position inside something. For instance, the builtin <code>file</code> object is its own iterator, because files have an intrinsic seek position (which you can manipulate with <code>file.seek()</code> and <code>file.tell()</code>). Other objects, which represent the totality of a collection, like <code>list</code>, return something other than themselves.</p> <p>So, your tree really more sounds like the latter rather than the former; It doesn't have a position attribute that represents which node it is on; it's all nodes at the same time, so it probably <em>shouldn't</em> have a <code>next()</code> method; <code>__iter__</code> needs to return something else.</p> <p>Which gets us to generators. When a normal function contains a <code>yield</code> statement, it is automatically not a function at all, it's a generator. The difference is that when you call a function, it's body is executed (and possibly returns a value). When you call a generator, it returns immedately, without executing the body at all; instead you get an <em>iterator</em>! when you iterate over that, the function body gets called; advancing to the next <code>yield</code> each time until it finally returns.</p> <p>So, putting it all together, </p> <pre><code>class t: def __init__(self): self.l = [] self.a = 0 def __iter__(self): # first, yield everthing every one of the child nodes would yield. for child in self.l: for item in child: # the two for loops is because there's multiple children, and we need to iterate # over each one. yield item # finally, yield self yield self </code></pre> <p>But, since what we're doing is iterating a sequence of iterators (and also one more thing, self), <code>itertools.chain</code> as in the accepted answer, really makes a lot of sense.</p>
    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.
    3. 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