Intermediate Course
Lesson 2 of 100

Closures — Functions That Remember

Learn how inner functions capture and remember variables from their enclosing scope — the mechanism that makes decorators work.

Pyfessor

Lesson Briefing

Pyfessor

"A closure is a function that remembers the variables from the scope in which it was created, even after that scope has finished executing. This sounds subtle but it's profound: it means a function carries its context with it wherever it goes. When a decorator wraps your function, the wrapper 'closes over' the original function — that's a closure. When make_threshold_check(20) returns a function that remembers 20, that's a closure. Today you master closures so decorators feel completely natural."

Pyfessor

"Hi! I'm Pyfessor. Ready to s-s-s-tart our coding journey?"

Pyfessor
    Pyfessor AI — Learn Python with Your AI Mentor