Implementing __iter__()
Lesson 4 of 13 in Coddy's Python Iterators course.
Creating a custom iterator in Python involves implementing the __iter__() method in a class. This method is a crucial part of the iterator protocol and allows you to define how your custom object behaves as an iterator.
The __iter__() Method
The __iter__() method has two main purposes:
- It makes an object iterable.
- It returns the iterator object itself.
Here's the basic structure of a class with an __iter__() method:
class MyIterator:
def __init__(self, data):
self.data = data
self.index = 0
def __iter__(self):
return self
Implementing __iter__()
To implement __iter__() in your custom class:
- Define the
__iter__()method within your class. - The method should return the iterator object itself (usually
self).
Here's an example of a simple custom iterator:
class CountDown:
def __init__(self, start):
self.start = start
def __iter__(self):
# Return the iterator object (self)
return self
def __next__(self):
if self.start <= 0:
raise StopIteration
self.start -= 1
return self.start + 1
# Using the custom iterator
countdown = CountDown(5)
for num in countdown:
print(num)
In this example, the __iter__() method returns self, making the CountDown object both an iterable and its own iterator.
Key Points
- The
__iter__()method is called when you use theiter()function on an object or when you use it in a for loop. - Returning
selfin__iter__()is common when the class implements both__iter__()and__next__(). - If your class only implements
__iter__(), it should return a separate iterator object that has a__next__()method.
By implementing __iter__(), you make your custom objects iterable, allowing them to be used in for loops and with other iterator-based operations in Python.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a custom iterator class called AlphabetIterator that generates uppercase letters of the English alphabet. The iterator should start from 'A' and continue up to a specified letter.
You are provided with the following:
- A single uppercase letter as input (e.g., 'E', 'K', 'Z')
Your AlphabetIterator class should:
- Implement the
__iter__()method to make the object iterable - Generate uppercase letters starting from 'A'
- Stop when it reaches the specified input letter (inclusive)
After implementing the AlphabetIterator class, create an instance of it using the input letter, and print each generated letter on a new line.
The input will be provided as a string containing a single uppercase letter.
Try it yourself
# Read input
end_letter = input().strip()
class AlphabetIterator:
def __init__(self, end_letter):
self.end_letter = end_letter
self.current_letter = 'A'
def __iter__(self):
return self
# TODO: Implement the __next__() method
# Create an instance of AlphabetIterator
alphabet_iter = AlphabetIterator(end_letter)
# TODO: Iterate through the AlphabetIterator and print each letter
# Note: Make sure to print each letter on a new line