Item 14: Iterator and next Function

class RangeIterator(object):
    def __init__(self, start, stop):
        self.i = start
        self.stop = stop
    def __next__(self):
        if self.i >= self.stop:
            raise StopIteration(self.i)
        i = self.i
        self.i += 1
        return i
i = RangeIterator(3, 5)
try:
    print(next(i, -1))
    print(next(i, -1))
    print(next(i, -1))
    print(next(i))
except StopIteration as e:
    print('Caught:', repr(e))