SoFunction
Updated on 2025-04-27

Detailed explanation of the iterator protocol for Python iterators and generators

Python is a powerful programming language that provides many convenient tools and features, and iterators are one of them. Iterators allow us to traverse the sequence without having to understand the underlying implementation details of the sequence.

1. What is an iterator

An iterator is an object that can traverse all elements in a container (such as lists, tuples, dictionaries, etc.).

The iterator object implements two basic methods:

  • __iter__(): This method returns the iterator object itself.
  • __next__(): This method returns the next element of the container. If there are no more elements, throwStopIterationabnormal.

This protocol is called the iterator protocol.

2. Iterator protocol

The iterator protocol defines two core methods:

  • __iter__()
  • __next__()

2.1 __iter__() method

__iter__()The method returns the iterator object itself.

This allows the container object to beiter()function call, thus returning an iterator.

2.2 __next__() method

__next__()The method returns the next element of the container.

This method should be thrown when there are no more elements in the containerStopIterationabnormal.

2.3 Sample code

Here is a simple iterator example:

class MyIterator:
    def __init__(self, data):
         = data
         = 0

    def __iter__(self):
        return self

    def __next__(self):
        if  < len():
            result = []
             += 1
            return result
        else:
            raise StopIteration

# Use a custom iteratormy_iter = MyIterator([1, 2, 3, 4])
for item in my_iter:
    print(item)

The running result is:

1
2
3
4

2.4 Advantages of iterators

Iterators have the following advantages:

  • Save memory: The iterator does not load all data at once, but returns only one data at a time.
  • Lazy evaluation: The iterator generates data when needed, effectively improving the performance of the program.
  • Infinite Sequence: Iterators can be used to represent infinite sequences, such as natural sequences, without consuming infinite memory.

3. Create a custom iterator

Creating a custom iterator is very simple, just implement it__iter__()and__next__()Just the method.

Here is an example of a custom iterator that generates a sequence of natural numbers starting from 0:

Sample code

class CountIterator:
    def __init__(self, start=0):
         = start

    def __iter__(self):
        return self

    def __next__(self):
         += 1
        return  - 1

# Use a custom iteratorcounter = CountIterator()
for _ in range(10):
    print(next(counter))

The running result is:

0
1
2
3
4
5
6
7
8
9

4. Advanced usage of iterators

4.1 Infinite Sequence

Iterators can be used to generate infinite sequences, such as Fibonacci sequences:

4.2 Sample code

class FibonacciIterator:
    def __init__(self):
        ,  = 0, 1

    def __iter__(self):
        return self

    def __next__(self):
        ,  = ,  + 
        return 

# Use Fibonacci sequence iteratorfib = FibonacciIterator()
for _ in range(10):
    print(next(fib))

The running result is:

1
1
2
3
5
8
13
21
34
55

4.3 File Iterator

We can create an iterator to read the file line by line:

4.4 Sample Code

class FileIterator:
    def __init__(self, filename):
         = open(filename, 'r')

    def __iter__(self):
        return self

    def __next__(self):
        line = ()
        if not line:
            ()
            raise StopIteration
        return ()

# Use file iteratorfile_iter = FileIterator('')
for line in file_iter:
    print(line)

5. Comprehensive detailed examples

Now, we will create a more complex example to show the practical application of the iterator.

This example will include a student management system where we can use iterators to iterate through student lists and implement common operations such as adding, deleting, and finding students.

5.1 Sample Code

class Student:
    def __init__(self, id, name, age):
         = id
         = name
         = age

    def __str__(self):
        return f'ID: {}, Name: {}, Age: {}'
  • student_iterator.py
class StudentIterator:
    def __init__(self, students):
         = students
         = 0

    def __iter__(self):
        return self

    def __next__(self):
        if  < len():
            student = []
             += 1
            return student
        else:
            raise StopIteration
  • student_manager.py
from student import Student
from student_iterator import StudentIterator

class StudentManager:
    def __init__(self):
         = []

    def add_student(self, id, name, age):
        student = Student(id, name, age)
        (student)

    def remove_student(self, id):
         = [s for s in  if  != id]

    def find_student(self, id):
        for student in :
            if  == id:
                return student
        return None

    def __iter__(self):
        return StudentIterator()

# Test the Student Management Systemmanager = StudentManager()
manager.add_student(1, 'Alice', 20)
manager.add_student(2, 'Bob', 22)
manager.add_student(3, 'Charlie', 21)

print('All students:')
for student in manager:
    print(student)

print('\nFind a student with student ID 2:')
print(manager.find_student(2))

print('\nRemove student with student ID 1:')
manager.remove_student(1)

print('\nAll students:')
for student in manager:
    print(student)

5.2 Operation results

All students:
ID: 1, Name: Alice, Age: 20
ID: 2, Name: Bob, Age: 22
ID: 3, Name: Charlie, Age: 21

Find students with student ID 2:
ID: 2, Name: Bob, Age: 22

Remove students with student ID 1:

All students:
ID: 2, Name: Bob, Age: 22
ID: 3, Name: Charlie, Age: 21

Summarize

Through this article, we introduce the iterator protocol in Python in detail, including__iter__()and__next__()method.

We learned how to create custom iterators, understood the basic concepts of generators, and demonstrated the importance of iterators in practical applications through an example of a comprehensive student management system.

Iterators have significant advantages in handling large data sets, saving memory and implementing lazy evaluation, and are an indispensable part of Python programming.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.