__next__
Dunder MethodPython 2.0+Intermediate
Returns the next value from an iterator; raises StopIteration when done
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
class Counter: def __init__(self, limit): self.limit = limit self.current = 0 def __iter__(self): return self def __next__(self): if self.current >= self.limit: raise StopIteration self.current += 1 return self.current for n in Counter(3): print(n)
__next__ returns the next value from an iterator; raises stopiteration when done. Implementing it lets you customize how Python interacts with your objects.
Try in PlaygroundTags
oopmagic-methodprotocolcore