Description and usage of python StopIteration exception
This article mainly introduces "the explanation and usage of python StopIteration exception". In the daily operation, I believe that many people have doubts about the explanation and use of python StopIteration exception. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "explanation and usage of python StopIteration exception". Next, please follow the editor to study!
1. StopIteration exceptions are used to complete identity iterations to prevent infinite loops.
2. _ _ next__ () completes the specified number of cycles and triggers the StopIteration exception to end the iteration.
Example
Class MyNumbers: def _ iter__ (self): self.a = 1 return self def _ next__ (self): if self.x < 20: X = self.a self.a + = 1 return x else: raise StopIterationmyclass = MyNumbers () myiter = iter (myclass) for x in myiter: print (x) so far The study on "explanation and usage of python StopIteration exception" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!