Python Global and Nonlocal Statements

This is useful when you want to modify a value that’s defined outside your current function scope.

– The `nonlocal` statement does something similar, but for variables that are defined in an enclosing function (i.e., the parent or grandparent of your current one). It allows you to access and modify those values without having to pass them as arguments or use global variables.

Now let’s see some examples! Here’s a simple program that uses both statements:

# This script demonstrates the use of the 'nonlocal' keyword in nested functions.

def outer():
    x = 10 # define a variable in the enclosing function scope
    
    def inner():
        nonlocal x # use 'nonlocal' to access and modify the value of 'x' defined in 'outer()'
        
        x += 5 # add 5 to the current value of 'x' (which is still defined as a global variable)
        
        print(f"The new value of x inside inner() is: {x}") # prints the updated value of 'x' within the nested function
    
    inner() # call the nested function and execute its code
    
    print(f"The final value of x outside outer() is: {x}") # prints the final value of 'x' after the nested function has been called and executed

outer() # call the outer function to run the entire script

# Output:
# The new value of x inside inner() is: 15
# The final value of x outside outer() is: 15

In this example, we define a variable `x` in the enclosing function scope (i.e., inside `outer()`) with an initial value of 10. Then, we create another function called `inner()`, which uses the `nonlocal` statement to access and modify the global variable `x`. Finally, we call `inner()` from within `outer()` and print both the new value of `x` inside `inner()` and its final value outside `outer()`.

Here’s what you should see when running this program:

# Define a global variable x and assign it a value of 10
x = 10

# Define a function called outer() that takes no parameters
def outer():
    # Use the nonlocal statement to access and modify the global variable x
    nonlocal x
    # Assign a new value of 15 to x
    x = 15
    # Define a function called inner() that takes no parameters
    def inner():
        # Print the new value of x inside inner()
        print("The new value of x inside inner() is: {}".format(x))
    # Call inner() from within outer()
    inner()
    # Print the final value of x outside outer()
    print("The final value of x outside outer() is: {}".format(x))

# Call outer() to execute the code
outer()

# Output:
# The new value of x inside inner() is: 15
# The final value of x outside outer() is: 15

As you can see, the global variable `x` has been modified by both functions! This can be useful in certain situations where you want to share data between multiple nested functions without having to pass it as an argument or use a global variable. However, I should warn you that using too many globals and nonlocals can make your code harder to read and understand, so try to avoid them whenever possible!

I hope this guide helped clarify the difference between `global` and `nonlocal` statements in Python! Let me know if you have any questions or comments.

SICORPS