English
Summary
Conclusions
An iterator separates obtaining the next element from processing it. A generator implements this contract with yield, while itertools combines ready-made traversal patterns. A lambda suits a short local rule; a decorator suits shared behavior around a call. In both cases, readability and an explicit contract matter more than reducing the line count.
Self-check questions
- How does an iterable differ from an iterator?
- Who handles
StopIterationin aforloop? - When does a generator function's body start executing?
- What state is preserved after
yield? - What do
yield from,send, andclosedo? - Why might summing a generator a second time give zero?
- How does
takewhilediffer fromfilter? - When is sorting before
groupbynecessary, and when is it harmful? - How does
batchedhandle an incomplete final batch? - What does
sys.getsizeofactually measure? - Why might a closure in a loop see the final value?
- How do you choose between
map, a comprehension, andreduce? - When is a decorator applied, and when does its wrapper execute?
- Why are
wrapsandParamSpecneeded? - How does decorator order change retries and timing?
Useful links
- Functional programming tools: https://docs.python.org/3.14/howto/functional.html.
- Iterators: https://docs.python.org/3.14/library/itertools.html.
- Decorators and caching: https://docs.python.org/3.14/library/functools.html.
- Operations as functions: https://docs.python.org/3.14/library/operator.html.