📅  最后修改于: 2022-03-11 14:46:23.955000             🧑  作者: Mango
>>> 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