Iterators & Generators Highlights Summary
Every highlight bullet from the 11 pages in this section, gathered on one page and grouped by the page it came from.
- for, with, and @decorator are syntax sugar over dunder-method protocols, not built-in special cases
- A generator function returns an object immediately; its body only runs incrementally, on demand
- Closures capture variables by reference to a shared cell, not by copying a value
- itertools and functools are composable building blocks over these same two ideas, not a separate system
- Iterable has iter; iterator has next
- for loops call iter() then next() until StopIteration
- Generators are the easiest custom iterators
- collections.abc documents iteration interfaces
- yield pauses function preserving local state
- Generators are single-pass iterators
- send/throw advanced but rarely needed
- Pipeline stages without intermediate lists
- yield from delegates to sub-iterator
- Forwards send/throw/close to sub-generator
- Flattens nested iteration cleanly
- Replaces manual for/yield loops
- itertools returns lazy iterators
- groupby requires sorted input by key
- islice slices iterators without indexing
- product replaces nested for loops
- lru_cache memoizes pure function calls
- partial binds arguments early
- wraps preserves decorator metadata
- singledispatch for type-based overloads
- Nested functions close over enclosing variables
- Late binding trap in loops creating lambdas
- nonlocal rebinds enclosing names
- Closures power decorators and factories
- Decorators are callables wrapping callables
- @decorator syntax applies at import time
- wraps copies name and doc
- Parametrized decorators need factory function
- Class decorators mutate or wrap class objects
- Method decoration order matters with descriptors
- dataclass is a class decorator
- Register plugins by decorating classes
- with guarantees cleanup via exit
- contextmanager decorator builds managers from generators
- ExitStack composes many managers dynamically
- exit receives exception info
- Prefer generators for large or streaming data
- Always use functools.wraps on decorators
- itertools over hand-rolled iterator loops
- Context managers for resource cleanup
NzA2MjAyfDEzN29pZ2N8b2kuc2VkaXVnZWRvYw==
Revisado por Chris St. John·Última actualización: 31 jul 2026