DEV Community

yosi
yosi

Posted on

Understanding the Difference between nonlocal and global in Python

Python, being a versatile and powerful programming language, provides two keywords, nonlocal and global, to access variables defined in outer scopes. While these keywords serve a similar purpose, they have distinct behaviors and use cases. In this blog post, we'll explore the difference between nonlocal and global in Python.

The nonlocal Keyword

The nonlocal keyword is used to indicate that a variable is defined in an outer function scope, often referred to as a non-local scope. It is particularly useful when working with nested functions. By using the nonlocal keyword, you can access and modify variables from the outer scope within an inner function.

Let's take a look at an example to illustrate its usage:

def outer_function():
    x = 10

    def inner_function():
        nonlocal x
        x = 20
        print(x)  # Output: 20

    inner_function()
    print(x)  # Output: 20

outer_function()
Enter fullscreen mode Exit fullscreen mode

In the above example, we have an outer function called outer_function(), which defines a variable x with an initial value of 10. Inside outer_function(), we have an inner function called inner_function(), where we declare x as a nonlocal variable using the nonlocal keyword. We then assign a new value of 20 to x and print it, resulting in the output of 20. The change in the value of x persists even after the inner_function() has finished executing, as seen by the subsequent print statement in outer_function().

The nonlocal keyword allows us to modify variables in the outer scope from within nested functions, providing a way to communicate and share data between the inner and outer functions.

The global Keyword

On the other hand, the global keyword is used to indicate that a variable is defined in the global scope, outside of any function. When a variable is declared as global, it can be accessed and modified from any part of the program, including within functions.

Consider the following example:

x = 10

def my_function():
    global x
    x = 20
    print(x)  # Output: 20

my_function()
print(x)  # Output: 20
Enter fullscreen mode Exit fullscreen mode

In this example, we have a global variable x with an initial value of 10. Inside the function my_function(), we declare x as a global variable using the global keyword. We then assign a new value of 20 to x and print it, resulting in the output of 20. The change made to the global variable inside the function is reflected throughout the program, as seen by the subsequent print statement outside the function.

The global keyword enables us to access and modify variables in the global scope from within functions. It provides a way to work with global variables while maintaining their values across different parts of the program.

Summary

The nonlocal keyword is used to access and modify variables in an outer (non-local) function scope, particularly within nested functions. It allows us to communicate and share data between the inner and outer functions.

On the other hand, the global keyword is used to access and modify variables in the global scope, outside of any function. It enables us to work with global variables and maintain their values across different parts of the program.

Top comments (2)

Collapse
 
r22wan2 profile image
R22wan2

bro can you explain 're' module

Collapse
 
yosi profile image
yosi