Course contentsShow
← Python
Lesson 266 of 1,0605. Advanced Iteration and ComprehensionsFree lesson

Separating Iterables from Iterators

You will distinguish between container objects that provide iterators and the iterator objects themselves to prevent common state-related bugs.

Separating Iterables from Iterators

In this lesson, you will learn why treating an iterable as its own iterator is a common trap that leads to "exhausted" data and hard-to-track bugs.

The Core Distinction

It is common to confuse the source of data (the iterable) with the cursor moving through that data (the iterator).

An iterable is any object (like a list or tuple) that can return an iterator. Crucially, calling iter() on an iterable produces a new iterator object every time. An iterator, however, is the object that maintains the state of your position. Once it reaches the end, it is "exhausted"—it cannot be reset.

The "Single-Use" Trap

If you create a custom iterator, you might be tempted to put the __iter__ and __next__ methods on the same class. This creates a stateful object where the data source and the cursor are fused together.

# The problematic approach: fusing iterable and iterator
class MyRange:
    def __init__(self, limit):
        self.limit = limit
        self.current = 0
    def __iter__(self):
        return self  # It returns itself!
    def __next__(self):
        if self.current >= self.limit:
            raise StopIteration
        self.current += 1
        return self.current

# Problem:
nums = MyRange(3)
print(list(nums))  # [1, 2, 3]
print(list(nums))  # [] - The iterator is already exhausted!

Because nums is both the iterable and the iterator, you cannot loop over it twice. To fix this, you must separate them. The __iter__ method should return a new instance of a separate iterator class, keeping the original object as a reusable data source.

Why It Matters

When you implement custom containers, always ensure your __iter__ method generates a fresh iterator instance. This allows your objects to be used in multiple for loops or comprehensions simultaneously without the internal state of one loop interfering with another. By keeping the "source" (iterable) distinct from the "cursor" (iterator), you ensure your code remains predictable, reusable, and free of silent data loss.

Key Takeaway: Never make an object its own iterator if you want it to be reusable; always have __iter__ return a fresh iterator object so the source data remains untouched.