In [1]:
%load_ext cython

In [2]:
%%cython
cdef class B:
    cdef int i, n
    def __iter__(self):
        self.i = 0
        self.n = 10
        return self
    def __next__(self):
        self.i += 1
        if self.i > self.n:
            raise StopIteration
        else:
            return self.i

In [3]:
b = B()
for x in b:
    print(x, end='')
print('\n\nAutomatic restarts!\n')
for x in b:
    print(x, end='')


12345678910

Automatic restarts!

12345678910

In [ ]: