- This topic is empty.
-
AuthorPosts
-
May 19, 2026 at 4:53 am #6614
Python provides a powerful mechanism called the iterator protocol that allows objects to be looped over using constructs like:
for item in obj: ...Many beginners see code like this and wonder what exactly it means:
def __iter__(self): self.n = 0 return selfThis post explains the idea from scratch in plain English.
What is
__iter__()?The
__iter__()method is part of Python’s iterator protocol.Whenever Python encounters:
for x in obj: print(x)Python internally does something similar to:
iterator = iter(obj)The
iter()function then calls:obj.__iter__()So the purpose of
__iter__()is:- To prepare the object for iteration
- To return an iterator object
Breaking Down the Code
def __iter__(self): self.n = 0 return selfLine 1: Resetting State
self.n = 0This initializes or resets a counter variable.
If the iterator is supposed to generate values one by one, the object needs some way to remember its current position.
For example:
0, 1, 2, 3, 4 ...The variable
self.nstores the current position during iteration.Resetting it to
0means:“Start iteration from the beginning.”
Line 2: Returning
selfreturn selfThis is the most important part.
It means:
“The current object itself will behave as the iterator.”
Instead of creating a separate iterator object, the same object handles iteration.
Why Can the Object Return Itself?
An iterator object must implement two methods:
__iter__()__next__()
Example:
class Counter: def __iter__(self): return self def __next__(self): ...Since the same class already contains both methods, the object can safely return itself.
Complete Working Example
class Numbers: def __iter__(self): self.n = 1 return self def __next__(self): if self.n <= 5: value = self.n self.n += 1 return value raise StopIterationUsage:
nums = Numbers() for x in nums: print(x)Output:
1 2 3 4 5
What Happens Internally?
Python roughly performs these steps:
nums = Numbers() iterator = nums.__iter__()Since
__iter__()returnsself, this becomes:iterator = numsPython then repeatedly calls:
iterator.__next__()until:
StopIterationis raised.
Iterable vs Iterator
These two terms are related but different.
Concept Meaning Iterable An object that can be looped over Iterator An object that produces values one at a time Sometimes they are separate objects.
Sometimes the same object performs both roles.
Using
return selfmeans the object itself acts as the iterator.
Important Detail
Returning
selfalone is not enough.The class must also implement:
def __next__(self):Without
__next__(), Python cannot retrieve the next value during iteration.
Real-World Example: File Objects
Python file objects work similarly.
f = open("data.txt") iter(f) is fThe file object itself behaves as its own iterator.
Key Takeaway
The statement:
return selfinside
__iter__()means:“Use this same object as the iterator.”
This design is common in Python when the iterable object itself already contains the iteration logic through
__next__(). -
AuthorPosts
- You must be logged in to reply to this topic.
