In Python, iterators are objects that implement the iterator protocol: the methods __iter__() and __next__(). Iterators manage their internal state and return the next element of the sequence with each call to next() or raise a StopIteration exception when finished.
Iterable objects are those over which you can iterate (for example, lists, strings, dictionaries). All of them have a method __iter__(), which returns an iterator.
To create your own iterator, you need to create a class with methods __iter__() (returns self) and __next__() (implements the logic of getting the next element):
class Counter: def __init__(self, low, high): self.current = low self.high = high def __iter__(self): return self def __next__(self): if self.current > self.high: raise StopIteration else: self.current += 1 return self.current - 1 for num in Counter(1, 3): print(num) # 1 2 3
What is the difference between iterable objects and iterators? Can you iterate over an iterator more than once?
Answer: An iterable object implements only the method __iter__() that returns an iterator. An iterator is an object that has the method __next__(). An iterator is usually iterated only once: after reaching the end of the sequence, re-iteration is not possible without creating a new iterator.
Story
In a log analysis project, a developer wrote a function that accepted an iterator and tried to iterate over it twice (
for log in logs:), expecting both iterations to yield the same results. The second iteration returned nothing because the iterator was already "exhausted".
Story
In one module, a developer returned not a list from a function but a generator, allowing a single iteration. When trying to pass it to another function that expected an iterable object for multiple passes, an unexpected error occurred, resulting in an empty outcome.
Story
In a Web API project, a developer tried to serialize an iterator directly to JSON. Iterators are not lists, and conversion is impossible without first converting them to a list (otherwise serialization will end in an exception).