title: What is the difference between raise StopIteration and a return statement in generators?
body: <p>I'm curious about the difference between using <code>raise StopIteration</code> and a <code>return</code> statement in generators.</p>
  
  <p>For example, is there any difference between these two functions?</p>
  
  <pre><code>def my_generator0(n):
      for i in range(n):
          yield i
          if i &gt;= 5:
              return
  
  def my_generator1(n):
      for i in range(n):
          yield i
          if i &gt;= 5:
              raise StopIteration
  </code></pre>
  
  <p>I'm guessing the more "pythonic" way to do it is the second way (please correct me if I'm wrong), but as far as I can see both ways raise a <code>StopIteration</code> exception.</p>