0
0
Pythonprogramming~10 mins

Nonlocal keyword in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nonlocal keyword
Define outer function
Define inner function
Inner function uses 'nonlocal'
Modify outer variable
Call inner function
Outer variable changed
Return or print result
The flow shows how an inner function accesses and changes a variable from its outer function using 'nonlocal'.
Execution Sample
Python
def outer():
    x = 5
    def inner():
        nonlocal x
        x = 10
    inner()
    return x
This code changes the outer variable x from 5 to 10 using the nonlocal keyword inside inner().
Execution Table
StepActionVariable 'x' ValueExplanation
1Define outer(), set x=55Outer function starts, x initialized to 5
2Define inner() inside outer()5Inner function created, no change yet
3Call inner()10Before inner runs, x is 5; after inner runs, x is 10
4Inside inner(), 'nonlocal x' declared10Inner function refers to outer x
5Assign x = 10 inside inner()10Outer x changed to 10 via nonlocal
6inner() ends, return to outer()10x remains 10 after inner finishes
7outer() returns x10Final value of x is 10, changed by inner()
💡 inner() finishes, outer() returns updated x=10
Variable Tracker
VariableStartAfter Step 5Final
x51010
Key Moments - 2 Insights
Why do we need 'nonlocal' inside inner() to change x?
Without 'nonlocal', assigning x inside inner() creates a new local x, not changing outer x. See step 4 and 5 where 'nonlocal x' links inner x to outer x.
What happens if we remove 'nonlocal' and assign x=10 inside inner()?
A new local variable x is created inside inner(), outer x stays 5. The execution_table would show x unchanged after inner() call.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of x right after step 3 (calling inner())?
AUndefined
B5
C10
DNone
💡 Hint
Check the 'Variable x Value' column at step 3 in the execution_table.
At which step does the variable x change from 5 to 10?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the step where 'Assign x = 10 inside inner()' happens in the execution_table.
If we remove 'nonlocal' from inner(), what will outer() return?
A5
B10
CError
DNone
💡 Hint
Refer to key_moments about what happens without 'nonlocal' and the variable_tracker showing x values.
Concept Snapshot
Nonlocal keyword lets inner functions modify variables in outer (but not global) scopes.
Syntax: use 'nonlocal var' inside inner function.
Without it, assignment creates a new local variable.
Use it to change outer variables from inner functions.
Common in nested functions to share state.
Full Transcript
This visual trace shows how the 'nonlocal' keyword works in Python. We start by defining an outer function with a variable x set to 5. Inside it, we define an inner function. When inner() runs, it declares 'nonlocal x' to tell Python that x refers to the outer variable. Then inner() changes x to 10. After inner() finishes, the outer function returns x, which is now 10. Without 'nonlocal', inner() would create a new local x and the outer x would stay 5. This trace helps understand how 'nonlocal' allows inner functions to modify variables in their enclosing scopes.