Quick Answer
A closure is a nested function that captures and remembers variables from its enclosing scope even after the outer function returns. The enclosed variables form a "cell" object. Closures are the mechanism behind decorators and factory functions. Variables must be declared nonlocal to be assigned inside a closure. Closures in loops: use default argument (i=i) to capture current value.
Answer
Closures capture variables from enclosing scopes. Used in decorators, factories, callbacks. inner() retains access to outer() variables even after outer() finishes.
S
SugharaIQ Editorial Team
Verified Answer
This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.