Iterators
__iter__ and __next__ methods
Interview Relevant: Python internals
Iterators
Objects that implement the iterator protocol.
Code Examples
python
1class Count:
2 def __init__(self, start, end):
3 self.current = start
4 self.end = end
5
6 def __iter__(self):
7 return self
8
9 def __next__(self):
10 if self.current > self.end:
11 raise StopIteration
12 val = self.current
13 self.current += 1
14 return val
15
16for i in Count(1, 3):
17 print(i)