class RangeIterator(object):
def __init__(self, start, end):
self.i = start
self.end = end
def __next__(self):
if self.i >= self.end:
raise StopIteration(self.i)
i = self.i
self.i += 1
return i
class Range(object):
def __init__(self, start, end):
self.start = start
self.end = end
def __iter__(self):
return RangeIterator(self.start, self.end)
r = Range(3, 10)
try:
s, t = iter(r), iter(r)
while True:
print(next(s), next(t))
except StopIteration as e:
print('Caught:', repr(e))
Item 15: Enumerable and iter function
Posted by Logan Chien