HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
To prevent the iteration to go on forever, we can use the
StopIteration
statement.
In the __next__()
method, we can add a terminating condition to raise an error if the iteration is done a specified number of times:
Stop after 20 iterations:
class MyNumbers:
def __iter__(self):
self.a =
1
return self
def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass =
MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)