What is the purpose of the nonlocal statement in a nested function?
Correct answer B. It lets the inner function rebind a variable in the nearest enclosing function scope
Explanation
Without a declaration, assigning to a name inside a function makes it local. nonlocal tells Python that the name refers to a binding in the nearest enclosing function scope, so it can be modified, as in closures that keep counters. global, by contrast, targets module-level names.
Answer checked How we write and check questions
Discussion