153. Implementing Custom Iterators
class Counter:
def __init__(self, start, end):
self.current = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.current >= self.end:
raise StopIteration
self.current += 1
return self.current - 1
# Usage
for number in Counter(1, 5):
print(number) # Output: 1, 2, 3, 4Last updated