在这段代码中,我有一个类名 Iter,它包含两个 dunder 方法__iter__和__next__. 在__iter__方法中,我设置self.current为零并返回self。在下一个方法中,我增加了
self.current += 1. 当它达到 10 时,我希望它引发StopIteration异常。
class Iter:
def __iter__(self):
self.current = 0
return self
def __next__(self):
self.current += 1
if self.current == 10:
raise StopIteration
return self.current
it = Iter()
for i in it:
print(i)