StopIteration
StopIteration 异常用于标识迭代的完成,防止出现无限循环的情况,在 __next__() 方法中我们可以设置在完成指定循环次数后触发 StopIteration 异常来结束迭代。
在 20 次迭代后停止执行:
实例(Python 3.0+)
classMyNumbers: def__iter__(self): self.a = 1 returnself def__next__(self): ifself.a <= 20: x = self.a self.a += 1 returnx else: raiseStopIterationmyclass = MyNumbers()myiter = iter(myclass)forxinmyiter: print(x)
执行输出结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20