All courses
· 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.

Pathaxis · Original content

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.

85 min 6 lessons 20 quiz questions 6 exercisesStart course

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

Syllabus

01

Functions as values

The idea both halves of this course are built on.

02

Decorators

Wrapping a function in behaviour without touching its body.

03

Lazy sequences

Producing values one at a time, and the pipelines that enables.