Why does a class need __iter__() to return an iterator?
<p>Why does a class need to define <code>__iter__()</code> returning self, to get an iterator of the class?</p>

<pre><code>class MyClass:
    def __init__(self):
        self.state = 0

    def __next__(self):
        self.state += 1
        if self.state &gt; 4:
            raise StopIteration
        return self.state

myObj = MyClass()
for i in myObj:
    print(i)
</code></pre>

<p>Console log:</p>

<pre><code>Traceback (most recent call last):
   for i in myObj:
TypeError: 'MyClass' object is not iterable
</code></pre>

<p>the answer <a href="https://stackoverflow.com/a/9884259/4515198">https://stackoverflow.com/a/9884259/4515198</a>, says </p>

<blockquote>
  <p>An iterator is an object with a next (Python 2) or <code>__next__</code> (Python 3) method. </p>
</blockquote>

<p>The task of adding the following:</p>

<pre><code>def __iter__(self):
   return self
</code></pre>

<p>is to return an <strong>iterator</strong>, or an object of the class, which defines the <code>__next__()</code> method.</p>

<p>But, isn't the task of returning an object of MyClass (which defines the <code>__next__()</code> method) already done by the <code>__new__()</code> method, when MyClass is instantiated in the line myObj = MyClass() ?</p>

<p>Won't the objects of a class defining <code>__next__()</code> method, be iterators by themselves?</p>

<p>I have studied the questions <a href="https://stackoverflow.com/q/35657927/4515198">What is the use of returning self in the __iter__ method?</a> and <a href="https://stackoverflow.com/q/19151/4515198">Build a Basic Python Iterator</a>, but I am still unable to understand the reason for having an <code>__iter__()</code> method returning self.</p>