Problem Statement
What makes an object iterable in Python?
Explanation
An object is iterable if it implements the iteration protocol. That means it defines dunder iter which returns an iterator. The iterator itself must implement dunder next.
In practice, containers return a fresh iterator from dunder iter, while custom iterator objects can return self when dunder iter is called.
Code Solution
SolutionRead Only
class Box:
def __init__(self, items): self.items=items
def __iter__(self): return iter(self.items)