Do generators simply make new objects with __iter__ and next functions?
<p>I tried to search for this answer on my own, but there was too much noise.</p>

<p>Are generators in python just a convenience wrapper for the user to make an iterator object?</p>

<p>When you define the generator:</p>

<pre><code>def test():
  x = 0
  while True:
    x += 1
    yield x
</code></pre>

<p>is python simply making a new object, adding the <code>__iter__</code> method, then putting the rest of the code into the <code>next</code> function?</p>

<pre><code>class Test(object):

  def __init__(self):
    self.x = 0

  def __iter__(self):
    return self

  def next(self):
    self.x += 1
    return self.x
</code></pre>

<p>No -- Generators also provide other methods (<code>.send</code>, <code>.throw</code>, etc) and can be used for more purposes than simply making iterators (e.g. coroutines).</p>

<p>Indeed, <code>generators</code> are an entirely different beast and a core language feature.  It'd be very hard (possibly impossible) to create one in vanilla python if they weren't baked into the language.</p>

<hr>

<p>With that said, <em>one</em> application of generators is to provide an easy syntax for creating an iterator :-).</p>

python