· intermediate
Decorators and Generators
Functions are values in Python, and two of its most useful features fall out of that. Wrap behaviour around a function without editing it, and produce sequences lazily instead of building them in memory.
Written in house, from the work rather than from a syllabus. Every example is one we would use ourselves, and every exercise is checked against the same engine that grades your answer.
What you’ll learn
- Pass and return functions, and capture state in a closure
- Write a decorator that works on any function, using *args and **kwargs
- Preserve a wrapped function's identity with functools.wraps
- Build a decorator that takes arguments of its own
- Produce values lazily with yield, and know when that matters
- Compose itertools pieces into a pipeline that never holds the whole sequence
Before you start
- · Comfortable writing functions with default and keyword arguments
- · Familiar with for loops and list comprehensions