Primary
''iterator'' ⚬|Definition|1st|20260605182851-00-⌔
Glossary — Python 3 documentation#term-iterator
iterator
An object representing a stream of data. Repeated calls to the iterator’s
__next__()method (or passing it to the built-in functionnext()) return successive items in the stream. When no more data are available aStopIterationexception is raised instead. At this point, the iterator object is exhausted and any further calls to its__next__()method just raiseStopIterationagain. Iterators are required to have an__iter__()method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as alist) produces a fresh new iterator each time you pass it to theiter()function or use it in aforloop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.More information can be found in Iterator Types.
CPython implementation detail: CPython does not consistently apply the requirement that an iterator define
__iter__(). And also please note that free-threaded CPython does not guarantee thread-safe behavior of iterator operations.Printed 2026-06-28.
(echo:: @ ᯤ)
Link to original
Secondary
• • •