| name | python-generators-iterators |
| description | Choose clear Python iteration patterns. Use this when deciding whether code should return a concrete collection versus a generator, or when designing generator functions, generator expressions, and custom iterator classes. |
| complexity | medium |
| risk_profile | ["ambiguity_sensitive"] |
| inputs | ["the collection being iterated or the work being performed","whether the collection is large, infinite, or expensive to compute","whether the caller needs to iterate once or multiple times","whether the iteration has side effects or dependencies on internal state","whether readability benefits from lazy evaluation or eager collection"] |
| outputs | ["a decision on whether a function should return a concrete collection or a generator","clear choice between generator function, generator expression, and custom iterator class","explicit documentation of exhaustion and side-effect expectations","a code review guide for iteration patterns"] |
| use_when | ["deciding whether a function should return a concrete list or a generator","choosing between a generator function, a generator expression, and a custom iterator class","reviewing yield and yield from patterns for readability and correctness","designing iteration behavior for single-pass versus multi-pass expectations","deciding when to implement __iter__ and __next__ protocol","reviewing iterator exhaustion and side effects on repeated iteration"] |
| do_not_use_when | ["the main task is async iteration, async generators, or async for; use python-async-await","the main task is control-flow branching; use python-control-flow","the main task is choosing between Enum, dataclass, ABC, or Protocol; use python-model-selection","the main task is package/module boundary design; use python-module-boundaries","the main task is deep test design for lazy evaluation; use python-testing-pytest"] |