iterator class using generator function It is sometimes necessary for a generator function to expose state. In those situations it is advised to wrap the generator function as method __iter__ in a class. Here is an example:

class C:
    def __iter__(self):
        for i in range(4):
            yield i            
            self.state = i


It is convenient to use this in a for loop since the for loop will call iter() on the object, however, the object itself is not an iterator, so you cannot do this:

c = C()
print(next(c))


Which is inconvenient. To get around this inconvenience I have come up with the following class:

class C:
    def __init__(self):
        self.next = self.__iter__().__next__
    def __iter__(self):
        for i in range(4):
            yield i            
            self.state = i
    def __next__(self):
        return self.next()


Usage example:

c = C()
while 1:
    try:
        num = next(c)
        print(num)
        if num == 2:
            print("state:", c.state)
    except Exception:
        break


Is this the best way to design the class? I haven't seen this technique used anywhere else so I'm wondering why everyone else isn't using it, since the C objects can be used both as iterators and as iterables.
Looks like a lot of extra code for very little gain, considering you can simply call iter() on an iterable object to return an iterator:

&gt;&gt;&gt; c = C()
&gt;&gt;&gt; i = iter(c)
&gt;&gt;&gt; next(i)
0
&gt;&gt;&gt; next(i)
1
&gt;&gt;&gt; next(i)
2
&gt;&gt;&gt; c.state
1


Also, note your new class C can have more than one iterator, with only 1 "state".  The first iterator is created automatically by the constructor, for use with the next(c) call.  Additional iterators are created each time you start looping over c, since iter(c) gets called and returns a new generator!

&gt;&gt;&gt; c = C()
&gt;&gt;&gt; next(c)
0
&gt;&gt;&gt; print(next(c), c.state)
1 0
&gt;&gt;&gt; print(next(c), c.state)
2 1
&gt;&gt;&gt; for x in c: print(x, c.state)       # Start a new iteration
...
0 1
1 0
2 1
3 2
&gt;&gt;&gt; print(next(c), c.state)             # Continue original iteration
3 2
&gt;&gt;&gt; next(c)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 9, in __next__
StopIteration