title: How to "chain" iterators?
body: <p>I'm trying to chain iterators together with one iterator reading from a master file and another iterator taking each line of the master file and processing another file depending on the output of the first.</p>
  
  <p>The working code that I have is as follows</p>
  
  <pre><code>class MasterReader(object):
  
      def __init__(self, filename):
          self.f = open(filename, "r")
  
      def __iter__(self):
          return self
  
      def __next__(self):
          line = self.f.readline().strip()
          if line == "":
              raise StopIteration
          return line
  
  
  class SubReader(object):
  
      def __init__(self, mr):
          self.mr = mr
  
      def __iter__(self):
          self._next()
          return self
  
      def _next(self):
          self.current = open(self.mr.__next__(), "r")
  
      def __next__(self):
          while True:
              line = self.current.readline().strip()
              if line == "":
                  self._next()
                  continue
              return line
  
  
  mr = MasterReader("master")
  sr = SubReader(mr)
  
  for line in sr:
      print(line)
  </code></pre>
  
  <p>Where <code>master</code> is a file containing lines of other files</p>
  
  <pre><code>file1
  file2
  </code></pre>
  
  <p><code>file1</code> contains</p>
  
  <pre class="lang-none prettyprint-override"><code>1.a
  1.b
  1.c
  </code></pre>
  
  <p><code>file2</code> contains</p>
  
  <pre class="lang-none prettyprint-override"><code>2.a
  2.b
  2.c
  </code></pre>
  
  <p>The output is </p>
  
  <pre class="lang-none prettyprint-override"><code>1.a
  1.b
  1.c
  2.a
  2.b
  2.c
  </code></pre>
  
  <p>Again what I have works, but feels wrong in that I have a <code>while</code> loop in <code>__next__</code> I'm having to manually check for the end of each sub file and explicitly calling the next line in the <code>master</code> file.</p>
  
  <p>Is there a better/more pythonic way of doing this? </p>
  
  <p>EDIT:
  This is a simplified problem of what I'm trying to accomplish.  In the real version <code>SubReader</code> is going to be threaded and I only want one <code>MasterReader</code>.  Actually this won't work for my threading project but want to make sure I'm generalizing iterators before diving deeper into a mess.</p>