Get the App
SLTechnology News&Howtos  ›  Development  › 

Description and usage of python StopIteration exception

Shulou Source: shulou.com Published: 2022-06-03 17:30:52 11月13日 Update

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!

Tags: Methods usage learning more help loops iterations practicality next examples articles logos times theories knowledge articles websites materials follow questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Microsoft Huawei Apple