Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the nonlocal keyword do in Python?
It allows a function to modify a variable defined in its nearest enclosing scope that is not global.
Click to reveal answer
intermediate
Why can't you use global to modify variables in an enclosing function?
global only affects variables at the module (global) level, not variables inside enclosing functions. nonlocal is needed for enclosing function variables.
Click to reveal answer
beginner
Given this code snippet, what will be printed?
def outer():
x = 5
def inner():
nonlocal x
x = 10
inner()
print(x)
outer()
It will print 10 because inner() changes x in the enclosing outer() function using nonlocal.
Click to reveal answer
intermediate
What error occurs if you use nonlocal on a variable that doesn't exist in any enclosing scope?
Python raises a SyntaxError because nonlocal requires the variable to exist in an enclosing function scope.
Click to reveal answer
beginner
Can nonlocal be used to modify global variables?
No. nonlocal only works with variables in enclosing functions. Use global to modify global variables.
Click to reveal answer
What does the nonlocal keyword allow you to do?
AModify a global variable
BModify a variable in the nearest enclosing function scope
CCreate a new local variable
DDelete a variable
✗ Incorrect
nonlocal lets you change variables in the nearest enclosing function, not global or local variables.
What happens if you use nonlocal on a variable not found in any enclosing scope?
APython raises a SyntaxError
BThe variable is created locally
CThe variable is created globally
DNothing happens
✗ Incorrect
Python requires the variable to exist in an enclosing function scope for nonlocal. Otherwise, it raises a SyntaxError.
Which keyword should you use to modify a global variable inside a function?
Aenclosing
Blocal
Cnonlocal
Dglobal
✗ Incorrect
global is used to modify variables at the module (global) level.
In this code, what will be printed?
def outer():
x = 1
def inner():
x = 2
inner()
print(x)
outer()
A2
BError
C1
DNone
✗ Incorrect
inner() creates a new local x, so outer()'s x remains 1.
Which of these is true about nonlocal?
AIt can only be used inside nested functions
BIt can be used anywhere in the program
CIt creates a new global variable
DIt deletes variables
✗ Incorrect
nonlocal is only valid inside nested functions to access variables in the outer function.
Explain how the nonlocal keyword works with an example.
Think about nested functions and changing variables outside the inner function.
You got /3 concepts.
What is the difference between nonlocal and global keywords?
Consider where the variable lives: inside a function or at the module level.
You got /3 concepts.
Practice
(1/5)
1. What does the nonlocal keyword do in Python?
easy
A. Declares a variable as global across all modules.
B. Allows an inner function to modify a variable from its outer function.
C. Creates a new local variable inside the inner function.
D. Prevents any changes to variables in the outer function.
Solution
Step 1: Understand variable scopes
Variables inside a function are local by default, and inner functions cannot change outer variables unless specified.
Step 2: Role of nonlocal
The nonlocal keyword allows the inner function to access and modify variables from the nearest enclosing function scope.
Final Answer:
Allows an inner function to modify a variable from its outer function. -> Option B
Inside increment(), count = count + 1 tries to read and write count. Without nonlocal, Python treats count as local, but it is used before assignment.
Step 2: Fix with nonlocal
Adding nonlocal count tells Python to use the count from counter(), allowing modification.
Final Answer:
Missing 'nonlocal count' inside increment() -> Option C
Quick Check:
Modify outer variable needs nonlocal = A [OK]
Hint: Add nonlocal before modifying outer variable inside inner function [OK]
Common Mistakes:
Using global instead of nonlocal
Ignoring variable scope causing UnboundLocalError
Returning count without incrementing properly
5. Consider this code:
def make_accumulator():
total = 0
def add(value):
nonlocal total
total += value
return total
return add
acc = make_accumulator()
print(acc(5))
print(acc(3))
print(acc(-2))
What is the output of this code?
hard
A. 5\n8\n6
B. 5\n3\n-2
C. 0\n5\n8
D. Error: nonlocal used incorrectly
Solution
Step 1: Understand closure with nonlocal
The function make_accumulator() returns add, which remembers total. The nonlocal total lets add update total each call.
Step 2: Trace calls to acc
First call: total=0+5=5, prints 5. Second call: total=5+3=8, prints 8. Third call: total=8+(-2)=6, prints 6.
Final Answer:
5
8
6 -> Option A
Quick Check:
Accumulator sums values using nonlocal total = B [OK]
Hint: nonlocal keeps state in nested function closures [OK]
Common Mistakes:
Expecting total to reset each call
Confusing nonlocal with global
Thinking output is separate values, not cumulative