Python __next__() Magic Method

Rate this post

Syntax

object.__next__(self)

The Python __next__ method returns an arbitrary element that represents the “next” element when you iterate over the object on which it is called. For example, if you iterate over my_object using for x in my_object, Python internally calls my_object.__next__() in each loop iteration to determine the next element.

Formally, the __next__() method implements the built-in next() function. For example, if you call next(x) an object x, Python internally calls x.__next__() to determine the next element of the iterable object 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 next()

The next(iterator) function is one of Python’s built-in functions—so, you can use it without importing any library.

Return Value: It returns the next value from the iterator you pass as a required first argument. An optional second argument default returns the passed default value in case the iterator doesn’t provide a next value.

Before we learn more about the __next__() dunder method, let’s have a look at a couple of basic next() examples:

users = ['Alice', 'Bob', 'Carl', 'David']

# convert the list to an iterator
users_iterator = iter(users)

x = next(users_iterator)
print(x)
# Output: 'Alice'

x = next(users_iterator)
print(x)
# Output: 'Bob'

x = next(users_iterator)
print(x)
# Output: 'Carl'

x = next(users_iterator)
print(x)
# Output: 'David'

Example Custom __next__()

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!