Closures & first-class functions
P29.functions-closures.01 · Audience: Guest, IT/ML learner, Language professional
Where next?
This module unlocks
P29.functions-closures.01 · Audience: Guest, IT/ML learner, Language professional
This module unlocks
Welcome to Functions, Decorators & Generators — the second pillar of the Advanced Python discipline. Everything in this pillar grows from one idea: in Python, a function is a value like any other. This module builds that idea up to the closure — a function that carries its own private state — and the factory pattern interviewers love to probe.
def statement creates a function object and binds it to a name. That object can be assigned, stored in a list, passed as an argument (sorted(words, key=len)), and returned from another function. Nothing special happens at the call site — a function is data until you add ().Why it matters — Sorting with key=, callbacks, decorators, functools — every advanced idiom starts from the fact that a def produces an object you can hand around.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Your turn: build a counter factory with nonlocal, then rebuild
composition and partial application by hand, and close with the interview
question every Python screen asks about closures.
Loading exercise…
Loading exercise…
Loading exercise…
By the end you should be able to predict, out loud, what a loop full of lambdas returns and why — and fix it two different ways without hesitating.
Loading your notes...