« Back to Index

[Python Custom Iterator]

View original Gist on GitHub

Tags: #python #iterator

Python Custom Iterator.py

class Foo:
    def __init__(self):
        self.current = 0
        self.limit = 5

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.limit:
            raise StopIteration
        else:
            self.current += 1
            return self.current - 1


for f in Foo():
    print(f)