class Square:
def __init__(self,start,stop):
self.value = start - 1
self.stop = stop
def __iter__(self):
return self
def next(self):
if self.value == self.stop:
raise StopIteration
self.value += 1
return self.value ** 2
for i in Square(1,4):
print i,
Which outputs
1 4 9 16
The typical Python iteration protocol:
for y in x...is as follows:from
http://www.boost.org/doc/libs/1_41_0/libs/python/doc/tutorial/doc/html/python/iterators.html