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 is the enclosing scope in Python?
The enclosing scope is the scope of a function that surrounds another nested function. It is the middle layer between the local scope of the inner function and the global scope.
Click to reveal answer
beginner
How does Python find a variable in nested functions?
Python looks for the variable first in the local scope, then in the enclosing scope (the outer function), then in the global scope, and finally in the built-in scope.
Click to reveal answer
intermediate
What keyword allows a nested function to modify a variable in the enclosing scope?
The nonlocal keyword lets a nested function change a variable defined in its enclosing function's scope.
Click to reveal answer
beginner
Example: What will this code print?
def outer():
x = 5
def inner():
print(x)
inner()
outer()
It will print 5 because the inner function accesses the variable x from the enclosing scope of outer.
Click to reveal answer
intermediate
Why can't a nested function modify a variable in the enclosing scope without nonlocal?
Without nonlocal, assigning to a variable inside a nested function creates a new local variable, so the enclosing variable remains unchanged.
Click to reveal answer
In Python, where does the enclosing scope refer to?
AThe scope of the outer function surrounding a nested function
BThe global scope of the program
CThe local scope inside the nested function
DThe built-in Python scope
✗ Incorrect
The enclosing scope is the scope of the outer function that contains the nested function.
Which keyword allows a nested function to modify a variable in its enclosing scope?
Anonlocal
Bglobal
Clocal
Denclose
✗ Incorrect
The nonlocal keyword lets a nested function modify variables in the enclosing scope.
What happens if you assign a value to a variable inside a nested function without using nonlocal?
AIt modifies the variable in the enclosing scope
BIt creates a new local variable inside the nested function
CIt causes a syntax error
DIt modifies the global variable
✗ Incorrect
Assigning without nonlocal creates a new local variable inside the nested function.
In the nested function, if a variable is not found locally, where does Python look next?
AGlobal scope
BBuilt-in scope
CIt raises an error immediately
DEnclosing scope
✗ Incorrect
Python looks in the enclosing scope after the local scope before checking global or built-in scopes.
What will this code print?
def outer():
x = 10
def inner():
x = 20
print(x)
inner()
print(x)
outer()
A20 and 20
B10 and 10
C20 and 10
D10 and 20
✗ Incorrect
The inner function prints 20 (its local x), but the outer x remains 10.
Explain what the enclosing scope is and how Python uses it when looking up variables in nested functions.
Think about the layers Python checks for a variable name.
You got /3 concepts.
Describe how to modify a variable in an enclosing scope from inside a nested function and why this requires a special keyword.
Consider what happens when you assign a value inside a nested function.
You got /3 concepts.
Practice
(1/5)
1. What does enclosing scope mean in Python functions?
easy
A. Inner functions can access variables from outer functions
B. Variables inside a function are always global
C. Functions cannot access variables outside their own block
D. Only global variables can be used inside functions
Solution
Step 1: Understand function nesting
In Python, functions can be defined inside other functions, creating an inner and outer function relationship.
Step 2: Access rules for variables
Inner functions can use variables defined in their outer functions, which is called the enclosing scope.
Final Answer:
Inner functions can access variables from outer functions -> Option A
Quick Check:
Enclosing scope = inner function accesses outer variables [OK]
Hint: Inner functions see outer variables unless shadowed [OK]
Common Mistakes:
Thinking all variables inside functions are global
Believing inner functions cannot use outer variables
Confusing local and global scopes
2. Which of the following is the correct syntax to modify an outer variable inside an inner function?
easy
A. Just assign the variable directly without any keyword
B. Use global keyword inside the inner function
C. Use nonlocal keyword inside the inner function
D. Use outer keyword inside the inner function
Solution
Step 1: Understand variable modification rules
To change a variable from an outer (enclosing) function inside an inner function, Python requires the nonlocal keyword.
Step 2: Differentiate from global
global is for variables at the module level, not enclosing functions.
Final Answer:
Use nonlocal keyword inside the inner function -> Option C
Quick Check:
Modify outer variable = use nonlocal [OK]
Hint: Change outer function vars with 'nonlocal' inside inner function [OK]
Common Mistakes:
Using 'global' instead of 'nonlocal' for outer function variables
Trying to assign without any keyword causing UnboundLocalError
Using a non-existent 'outer' keyword
3. What is the output of this code?
def outer():
x = 5
def inner():
return x + 3
return inner()
print(outer())
medium
A. 3
B. 5
C. Error
D. 8
Solution
Step 1: Trace variable usage
Variable x is defined in outer() as 5. The inner function returns x + 3, which is 8.
Step 2: Function call and return
outer() calls inner() and returns its result, so print(outer()) prints 8.
Final Answer:
8 -> Option D
Quick Check:
Inner uses outer x=5, returns 5+3=8 [OK]
Hint: Inner function can read outer variables directly [OK]
Common Mistakes:
Expecting an error due to variable scope
Confusing inner return value with outer variable
Thinking inner cannot access outer variables
4. What is wrong with this code?
def outer():
x = 10
def inner():
x = x + 5
return x
return inner()
print(outer())
medium
A. It will print 15
B. UnboundLocalError because inner tries to modify x without nonlocal
C. SyntaxError due to wrong indentation
D. NameError because x is not defined anywhere
Solution
Step 1: Analyze variable assignment in inner()
Inside inner(), x = x + 5 tries to modify x. Python treats x as local but it is used before assignment.
Step 2: Understand error cause
This causes an UnboundLocalError because x is referenced before assignment locally. The fix is to declare nonlocal x.
Final Answer:
UnboundLocalError because inner tries to modify x without nonlocal -> Option B
Quick Check:
Modify outer var inside inner needs nonlocal [OK]
Hint: Assigning outer var inside inner needs 'nonlocal' keyword [OK]
Common Mistakes:
Assuming it prints 15 without error
Confusing UnboundLocalError with NameError
Ignoring the need for 'nonlocal' when modifying outer vars