Syntax
object.__iter__(self)
Background iter()
Python’s built-in iter()
function returns an iterator for the given object. For example, iter([1, 2, 3])
creates an iterator for the list [1, 2, 3]
.
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 iter()
example:
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 initializecurrent_index
with zero, so we start iterating with the first index ofdata
.__next__()
returns the next value after one iteration. We increment thecurrent_index
attribute to keep track of the current index of the element indata
.
Let’s create a Data
object 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 __next__()
):
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., 1
, 'Alice'
, 42
, and '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.