title: A simple Python iterator going in to infinite loop
body: <p>I was trying to write a simple countdown iterator of my own, I implemented a <code>__iter__()</code> function and the corresponding <code>__next__()</code> to support the iterator.I have used a <code>yield</code> function inside the <code>__next__()</code> function to return a new value everytime I iterate over the object.
  When I use <code>yield</code>, the code goes over in an infinite loop as compared to using <code>return</code> statement.
  Following is my code:</p>
  
  <pre><code>class MyIterator():
      def __init__(self,value):
          self.value = value
      def __iter__(self):
          return self
      def __next__(self):
          print("In the next function")
          if self.value &gt; 0:
              yield self.value
              self.value -= 1
          else:
              raise StopIteration("Failed to proceed to the next step")
  
  
  
  if __name__ == '__main__':
      myIt = MyIterator(10)
      for i in myIt:
          print(i)
  </code></pre>
  
  <p>And the O/P is  as follows:</p>
  
  <pre><code>  &lt;generator object __next__ at 0x101181990&gt;
    &lt;generator object __next__ at 0x1011818e0&gt;
    &lt;generator object __next__ at 0x101181990&gt;
    &lt;generator object __next__ at 0x1011818e0&gt;
  
    and so on for infinite times....
  </code></pre>