Python defining an iterator class, failed with "iter() returned non-iterator of type 'Fib'"
<p>I'm using python 2.7 and ipython2.7.
In ipython I tried:</p>

<pre><code>class Fib(object):
    def __init__(self, max):
        super(Fib, self).__init__()
        self.max = max

    def __iter__(self):
        self.a = 0
        self.b = 1
        return self

    def __next__(self):
        fib = self.a
        if fib &gt; self.max:
            raise StopIteration
        self.a, self.b = self.b, self.a + self.b
        return fib

def main():
    fib = Fib(100)
    for i in fib:
        print i

if __name__ == '__main__':
    main()
</code></pre>

<p>Well it reports an error:</p>

<p>TypeError                                 Traceback (most recent call last)
     in ()
        22
        23 if <strong>name</strong> == '<strong>main</strong>':
    ---> 24     main()
        25</p>

<pre><code>&lt;ipython-input-21-f10bd2d06666&gt; in main()
    18 def main():
    19     fib = Fib(100)
---&gt; 20     for i in fib:
    21         print i
    22

TypeError: iter() returned non-iterator of type 'Fib'
</code></pre>

<p>This piece of code is actually from internet. The gramma seems OK to me, but how the problem happens?</p>

<p>Thanks.</p>