Example 2: Building a Fibonacci Iterator
We’ll create an iterator that generates Fibonacci numbers up to a specified limit.
- Class Definition:
FibonacciIterator
is defined with an__init__
method that initializes the starting values of the Fibonacci sequence (self.a
andself.b
) and the limit up to which the numbers should be generated. - Iterator Method: The
__iter__
method returns the iterator object itself. - Next Method: The
__next__
method generates the next number in the Fibonacci sequence. If the current number exceeds the specified limit, it raises aStopIteration
exception. - Using the Iterator: The iterator can be used with a
for
loop or thenext()
function to generate and print Fibonacci numbers up to the specified limit.
class FibonacciIterator:
def __init__(self, limit):
self.limit = limit
self.a = 0
self.b = 1
def __iter__(self):
return self
def __next__(self):
if self.a > self.limit:
raise StopIteration
current = self.a
self.a, self.b = self.b, self.a + self.b
return current
# Create an instance of FibonacciIterator
fib_iterator = FibonacciIterator(100)
# Using a for loop to iterate
print("Using for loop:")
for num in fib_iterator:
print(num)
# Using next() to iterate
print("\nUsing next() function:")
fib_iterator = FibonacciIterator(100)
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
Output:
Using for loop:
0
1
1
2
3
5
8
13
21
34
55
89
Using next() function:
0
1
1
2
3
5
8
13
21
34
55
89
Conclusion
Building a custom iterator in Python is a powerful way to create more complex and memory-efficient data traversal mechanisms. By implementing the __iter__ and __next__ methods, you can control the iteration logic and manage state between iterations. The example provided here demonstrates how to create a simple iterator that generates square numbers, but the principles can be applied to more complex scenarios as well.
How to Build a basic Iterator in Python?
Iterators are a fundamental concept in Python, allowing you to traverse through all the elements in a collection, such as lists or tuples. While Python provides built-in iterators, there are times when you might need to create your own custom iterator to handle more complex data structures or operations. In this article, we will explore how to build a basic iterator in Python from scratch.