Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.