title: In python, how do I make a class a generator?
body: <p>I'm working with the varnish API in varnish 4.  The way it produces log entries is by Dispatch'ing against it, and passing in a callback.  Simple version:</p>
  
  <pre><code>class vlog(object):
      def __init__(self):
          self.body = []
      def callback(self, vap, cbd, priv):
          self.body.append(cbd)
      def main(self):
          self.vlog = varnishapi.VarnishLog(sopath="libvarnishapi.so")
          while True:
              ret = self.vlog.Dispatch(self.callback)
              if ret == 0:
                  time.sleep(0.1)
  </code></pre>
  
  <p>I need to do several more things in self.callback to aggregate the data properly, but whatever.</p>
  
  <p>My REAL question is this:  How can I turn the above class into a generator?  My ideal usage would be something like this:</p>
  
  <pre><code>    vlog_inst = vlog()
      for log_aggregate in vlog_inst:
          pass
  </code></pre>
  
  <p>Simply putting a 'yield' statement in the callback function never triggers iteration.  (surprising to me, adding the yield statement also causes all of my print statements to produce no output as well... I'm obviously missing something.)</p>