You can then iterate over all elements in the iterator, one element at a time, in a for or while loop such as:
for x in iter([1, 2, 3]).
Before we learn more about the
__iter__() dunder method, let’s have a look at a basic
customers = ['Alice', 'Bob', 'Carl', 'Dave', 'Elena', 'Frank'] iterator = iter(customers) print(next(iterator)) print(next(iterator)) for x in iterator: print(x)
You can view an explanation and the output of this on our detailed blog tutorial here:
Example Custom __iter__()
class Data: def __init__(self, data): self.data = data # an iterable def __iter__(self): self.current_index = 0 return self def __next__(self): if self.current_index < len(self.data): x = self.data[self.current_index] self.current_index += 1 return x raise StopIteration
__init__()initializes the data attribute that is expected to be an iterable.
__iter__()returns the iterator object — the one that implements the
__next__()method. In our case, this is the Data object on which it is called itself. We initialize
current_indexwith zero, so we start iterating with the first index of
__next__()returns the next value after one iteration. We increment the
current_indexattribute to keep track of the current index of the element in
Let’s create a
d and an iterator over the data object using the built-in
iter() function (that internally calls
__iter__())—and start iterating over the object using the built-in
next() function (that internally calls
d = Data([1, 'Alice', 42, 'finxter']) # Create an iterator iterator = iter(d) # Dynamically generate the next values - iterate! print(next(iterator)) print(next(iterator)) print(next(iterator)) print(next(iterator)) print(next(iterator))
The output is as follows: The first four calls result in the expected elements of the data attribute, i.e.,
'finxter'. The fifth call of
next() results in a
StopIteration error because we have finished iterating over all elements.
1 Alice 42 finxter Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 34, in <module> print(next(iterator)) File "C:\Users\xcent\Desktop\code.py", line 14, in __next__ raise StopIteration StopIteration
TypeError: ‘…’ object is not iterable
If you call
iter(x) on an object on which the
x.__iter__() dunder method is not defined, Python will raise a
TypeError: '...' object is not iterable.
To fix this error, simply define the
__iter__() method in the class definition before calling
iter() on an object—and make sure that
__iter__() returns an iterator object on which the dunder method
__next__() is defined!
Here’s an example:
class Data: def __init__(self, data): self.data = data # an iterable d = Data([1, 'Alice', 42, 'finxter']) # Create an iterator iterator = iter(d)
Here’s the error message:
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 10, in <module> iterator = iter(d) TypeError: 'Data' object is not iterable
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.