Closures

nested functions, LEGB scope, nonlocal, and the late-binding trap

30 min · interactive lesson with runnable code and 6 graded exercises.

What you'll be able to do

  • Explain the LEGB lookup order
  • Define a closure and show what it remembers
  • Use nonlocal to update enclosing state
  • Spot and fix late binding in loops

The full lesson is free with an account.

Nested functions & LEGB

What is a closure?

nonlocal

Closures in practice

The late-binding trap

Common pitfalls