SoFunction
Updated on 2025-03-02

A brief analysis of the wonderful uses of global and nonlocal keywords in Python

When writing functions in Python, you often encounter situations where you need to access and modify external variables inside the function. In this case, we can use the global and nonlocal keywords to declare the scope of variables so that these variables are accessed and modified correctly. This article will explore the usage of global and nonlocal in depth, including detailed sample code and practical application scenarios.

global keywords

In Python, the global keyword is used to declare global variables inside a function. When using a variable inside a function and you want the variable to be visible and modified outside the function, you need to use the global keyword.

Sample code:

x = 10

def increment_global():
    global x
    x += 1
    print("The value of x inside the function:", x)

increment_global()
print("The value of x outside the function:", x)

In the example above, the global keyword declares that the variable x is a global variable, so it can be modified inside the increment_global function.

nonlocal keyword

Unlike the global keyword, the nonlocal keyword is used to declare variables in external nested scopes in nested functions. When another function is defined inside one function and you want the inner function to have access to the variables of the external function, you need to use the nonlocal keyword.

Sample code:

def outer_function():
    y = 20

    def inner_function():
        nonlocal y
        y += 1
        print("The value of the internal function y:", y)

    inner_function()
    print("The value of the external function y:", y)

outer_function()

In the example above, the nonlocal keyword declares that the variable y is a variable in the external function outer_function, so it can be modified inside inner_function.

Practical application scenarios

1. Modify global variables inside the function

Sometimes it is necessary to modify global variables inside the function, such as counters and other application scenarios.

count = 0

def increment_counter():
    global count
    count += 1
    print("Current counter value:", count)

increment_counter()
increment_counter()

2. Access variables of external functions in nested functions

When another function is defined inside one function and the internal function needs to access the variables of the external function, the nonlocal keyword can be used.

def outer_function():
    result = 0

    def inner_function():
        nonlocal result
        result += 10
        print("Inner function calculation result:", result)

    inner_function()
    print("External function calculation result:", result)

outer_function()

3. Use external variables in closures

A closure is a special function that can access variables defined in its external scope. Use the nonlocal keyword to modify the value of an external variable in a closure.

def outer():
    x = 10

def inner():
    nonlocal x
    x += 5
    return x

  return inner

closure = outer()
print("Value in closure:", closure())

Sample code

1. Manage global variables using global keywords

global_var = 100

def modify_global():
    global global_var
    global_var += 50

modify_global()
print("Modified global variable value:", global_var)

In the above code, a global variable global_var is declared, and then the global keyword is used inside the function to modify its value, thereby realizing the management of global variables.

2. Use the nonlocal keyword to access variables of external functions

def outer_function():
    outer_var = 10

    def inner_function():
        nonlocal outer_var
        outer_var += 5
        print("External variable value in internal function:", outer_var)

    inner_function()
    print("External variable value in external function:", outer_var)

outer_function()

In this example, an external function outer_function is defined, which contains a variable outer_var. In the inner function inner_function, the variable outer_var in the external function is accessed and modified using the nonlocal keyword, and the modified value is printed.

3. Use a combination of global and nonlocal keywords

global_var = 100

def outer_function():
    nonlocal_var = 50

    def inner_function():
        global global_var
        nonlocal nonlocal_var

        global_var += 10
        nonlocal_var += 20

        print("Global variable value:", global_var)
        print("Non-local variable value in external functions:", nonlocal_var)

    inner_function()
    print("Global variable value (modified in external functions):", global_var)
    print("Non-local variable value in external function (modified in external function):", nonlocal_var)

outer_function()

This example demonstrates the use of both global and nonlocal keywords in nested functions. The internal function inner_function is modified and the nonlocal_var in the external function nonlocal_var is printed. At the same time, the modified values ​​of these variables are also printed in the external function.

Summarize

The global and nonlocal keywords in Python play an important role in variable scope and value passing. The global keyword is used to declare global variables inside a function, so that the value of the global variable can be accessed and modified within the function. The nonlocal keyword is used to declare variables in external nested scopes in nested functions, so that internal functions can access and modify variables in external functions.

This is the end of this article about a brief analysis of the magical uses of global and nonlocal keywords in Python. For more related Python global nonlocal content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!