Part 1 · Section 07

Scopes, Closures and Decorators

Functions in Python are objects, and they can be created inside other functions. Put those two facts together with Python’s rules for looking up names and you get closures: functions that remember variables from the place where they were defined. Closures, in turn, are the engine behind decorators, the @something lines you see above functions and classes in almost every real code base.

This section builds that chain one link at a time, and each idea depends on the previous one, so it pays to go in order.

In this section you’ll learn:

Lectures

  1. 01Global and Local Scopes✓
  2. 02Nonlocal Scopes✓
  3. 03Closures✓
  4. 04Closure Applications - Part 1✓
  5. 05Closure Applications - Part 2✓
  6. 06Decorators - Part 1✓
  7. 07Decorator Application - Timer✓
  8. 08Decorator Application - Logger, Stacked✓
  9. 09Decorator Application - Memoization✓
  10. 10Decorators - Part 2✓
  11. 11Decorator Application - Decorator Class✓
  12. 12Decorator Application (Decorating Classes)✓
  13. 13Decorator Application - Single Dispatch✓

Practice: quiz & exercises

12 quiz questions · 4 coding exercises

Go to practice →