Module Overview
Generators, Iterators, and Decorators
This module covers four related but distinct tools: generators (functions that produce values lazily, one at a time), the more general concept of iterators, decorators (functions that wrap other functions to add behavior), and two smaller but handy topics: lambdas and the datetime module.
Why This Matters
Generators let you work with huge or even infinite sequences without computing everything upfront. Decorators are everywhere in real Python code. You’ve already used @property, @classmethod, and @staticmethod in the OOP module; now you’ll see how a decorator like that actually works under the hood, and build your own.
What You’ll Learn
- Generator functions,
yield, and thenext()function - Iterators, the
iter()function, andenumerate - How to write your own decorators, and why
functools.wrapsmatters - Lambdas, and basic date/time handling with the
datetimemodule
Outcome
By the end of this module, you’ll be able to write lazy, memory-efficient generators and wrap existing functions with your own custom decorators.
Let’s get started.