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