A Fibonacci Iterator
Explore how to build a Fibonacci iterator from scratch in Python by defining a class with __init__, __iter__, and __next__ methods. Understand how iterators work with for loops and the role of the StopIteration exception in terminating iteration gracefully.
We'll cover the following...
We'll cover the following...
Now you’re ready to learn how to build an iterator. An iterator is just a class that defines an __iter__() method.
① To build an iterator from scratch, Fib needs to be a class, not a function.
② “Calling” Fib(max) is really creating an instance of this class and calling its __init__() method with max. The __init__() method saves ...