1class RangeTen:
2 def __init__(self, *args): pass # optional
3
4 def __iter__(self):
5 '''Initialize the iterator.'''
6 self.count = -1
7 return self
8
9 def __next__(self):
10 '''Called for each iteration. Raise StopIteration when done.'''
11 if self.count < 9:
12 self.count += 1
13 return self.count
14
15 raise StopIteration
16
17for x in RangeTen():
18 print(x) # 0, 1, ..., 9