Python __iter__() Magic Method

Syntax

object.__iter__(self)

The Python __iter__ method returns an iterator object. An iterator object is an object that implements the __next__() dunder method that returns the next element of the iterable object and raises a StopIteration error if the iteration is done.

Formally, the __iter__() method implements the built-in iter() function. For example, if you call iter(x) an object x, Python internally calls x.__iter__() to determine the iterable object associated with x.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

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__()

In the following example, you create a custom class Data and overwrite the __init__(), __iter__(), and __next__() methods so that you can create your own iterator over a Data object.

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_index with zero, so we start iterating with the first index of data.
  • __next__() returns the next value after one iteration. We increment the current_index attribute to keep track of the current index of the element in data.

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

If you hadn’t defined the __iter__() method, Python would’ve raised an error:

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

References:

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.

Join the free webinar now!