您现在的位置是:课程教程文章
python StopIteration异常的使用
2023-12-13 23:29课程教程文章 人已围观
1、StopIteration异常用于完成标识迭代,防止循环。
2、__next__()完成指定循环次数,触发StopIteration异常结束迭代。
实例
classMyNumbers: def__iter__(self): self.a=1 returnself def__next__(self): ifself.x<20: x=self.a self.a+=1 returnx else: raiseStopIteration myclass=MyNumbers() myiter=iter(myclass) forxinmyiter: print(x)
以上就是python StopIteration异常的使用,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
课程教程:python StopIteration异常的使用上一篇:c语言中空指针是什么
下一篇:没有了