Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Range-like iterator

class Range():
    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
        v = self.current
        self.current += 1
        return v
import it

r = it.Range(1, 4)
for n in r:
    print(n)

print('---')

for n in it.Range(2, 5):
    print(n)

Output:

1
2
3
---
2
3
4