Iterators and Generators Intro
On this page
Iterator Mental Model
An iterator produces values one at a time. This enables streaming processing and predictable memory usage.
Iterator Basics
it = iter([1, 2, 3]) print(next(it)) print(next(it))
Generator Function
def read_lines(path: str):
with open(path, "r", encoding="utf-8") as f:
for line in f:
yield line.rstrip("
")
Pipeline Style
def only_errors(lines):
for line in lines:
if "ERROR" in line:
yield line
errors = only_errors(read_lines("app.log"))
for e in errors:
print(e)
Operational Checklist
- Prefer generators for large inputs (logs, files, streams).
- Keep generators side-effect aware (resource management, ordering).
- Document whether a function returns a list or an iterator.
Failure Modes
- One-shot iterators: iterators are consumed; reusing requires re-creation.
- Hidden I/O: generator that reads files can fail late; handle errors intentionally.