Custom Iterator Class (__iter__ and __next__) Dry Run in PYTHON

Custom Iterator Class (__iter__ and __next__) is an interactive PYTHON dry run visualizer from the Advancedpython programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Custom Iterator Class (__iter__ and __next__) Program Code

class Fibonacci:
    def __init__(self, limit):
        self.limit = limit
        self.a, self.b = 0, 1
        self.count = 0
        
    def __iter__(self):
        return self
        
    def __next__(self):
        if self.count < self.limit:
            res = self.a
            self.a, self.b = self.b, self.a + self.b
            self.count += 1
            return res
        raise StopIteration

for val in Fibonacci(5):
    print(val)

View the complete Advancedpython programs page.

Program Console Custom Iterator Class (__iter__ and __next__) Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.