Distinguishing global, local, and nonlocal variables in Python

This post is lesson 16 of 54 in the subject Python Programming Language

1. Local Variables in Python

Local variables are variables you create within the body of a function. You can only use these variables within that function. For example:

def myFunction():
    # local variable
    s = "Gochocit.com"
    print(s)

myFunction()

Result

Gochocit.com

In the above example, variable s is a local variable and can only be used within the function myFunction(). If you use variable s outside of the function myFunction(), you will receive an error.

def myFunction():
    # local variable
    s = "Gochocit.com"
    print(s)

print(s)

Result

NameError: name 's' is not defined

2. Global Variables in Python

Global variables in Python are variables you create outside of any function and are in the global scope. You can use global variables throughout the entire program, both inside and outside of any function. For example:

# global variable
s = "Gochocit.com"
def myFunction():
    print("Inside myFunction", s)

myFunction()
print("Outside myFunction", s)

Result

Inside myFunction Gochocit.com
Outside myFunction Gochocit.com

Variable s is a global variable and can be used both inside and outside the function myFunction().

What happens if a local variable and a global variable have the same name? For example:

# global variable
s = "Gochocit.com"
def myFunction():
    #local variable
    s = "Hello all!"
    print(s)

myFunction()
print(s)

Result

Hello all!
Gochocit.com

The variable s created in the function myFunction() is a local variable. When printing the variable s in the function myFunction(), the program will use the local variable instead of the global variable.

Is it possible to change the value of a global variable inside a function? For example:

# global variable
s = "Gochocit.com"
def myFunction():
    #local variable
    s = s + "is great!"
    print(s)

myFunction()

Result

UnboundLocalError: local variable 's' referenced before assignment

The result is the UnboundLocalError error. This error occurs because Python sees the variable s as a local variable. And when the variable s has not been assigned a value yet but performs the + operator with another string, an error occurs. If you modify it as follows, there will be no error:

# global variable
s = "Gochocit.com"
def myFunction():
    #local variable
    s = "All"
    s = s + " is great!"
    print(s)

myFunction()

Result

All is great!

Note: The variable s in the function myFunction() is still considered a local variable in Python.

If you want to specify that a variable in the function is a global variable rather than a local variable, you can use the keyword global. For example:

# global variable
s = "Gochocit.com"
def myFunction():
    global s
    s = s + " is great!"
    print("Inside function:", s)

myFunction()
print("Outside function:", s)

Result

Inside function: Gochocit.com is great!
Outside function: Gochocit.com is great!

Now, Python will know that the variable s is a global variable that has been declared outside the function, not a local variable anymore.

3. Nonlocal Variables in Python

Nonlocal variables are used in nested functions. At this point, the scope of the variable is not defined. This means that the variable cannot be within the local or global scope. Use the keyword nonlocal to create nonlocal variables. For example:

def outerFunction():
    x = "Gochocit.com"

    def innerFunction():
        nonlocal x
        x = "Hello all!"
        print("inner:", x)

    innerFunction()
    print("outer:", x)

outerFunction()

Result

inner: Hello all!
outer: Hello all!

Note: If we change the value of a nonlocal variable, the local variable will also change.

5/5 - (1 vote)
Previous and next lesson in subject<< Parameters of a function in PythonHow to Create a Recursive Function in Python? >>

Leave a Reply

Your email address will not be published. Required fields are marked *