>>> class SquaresTo:
... def __init__(self, n):
... self.n = n
... def __iter__(self):
... for i in range(self.n):
... yield i * i
...
>>> s = SquaresTo(5)
>>> tuple(s)
(0, 1, 4, 9, 16)
>>> list(s)
[0, 1, 4, 9, 16]
>>> sum(s)
30