Open In App

How to Fix – UnboundLocalError: Local variable Referenced Before Assignment in Python

Last Updated : 29 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Developers often encounter the UnboundLocalError Local Variable Referenced Before Assignment error in Python. In this article, we will see what is local variable referenced before assignment error in Python and how to fix it by using different approaches.

What is UnboundLocalError: Local variable Referenced Before Assignment?

This error occurs when a local variable is referenced before it has been assigned a value within a function or method. This error typically surfaces when utilizing try-except blocks to handle exceptions, creating a puzzle for developers trying to comprehend its origins and find a solution.

Syntax:

UnboundLocalError: local variable 'x' referenced before assignment

Below, are the reasons by which UnboundLocalError: Local variable Referenced Before Assignment error occurs in Python:

  • Nested Function Variable Access
  • Global Variable Modification

Nested Function Variable Access

In this code, the outer_function defines a variable ‘x’ and a nested inner_function attempts to access it, but encounters an UnboundLocalError due to a local ‘x’ being defined later in the inner_function.

Python3
def outer_function():
    x = 10

    def inner_function():
        print(x)  # Trying to access 'x' from the outer function

        x = 20    # Inner function's local variable
        print(x)

    inner_function()

outer_function()

Output

 Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 12, in <module>
    outer_function()
  File "Solution.py", line 10, in outer_function
    inner_function()
  File "Solution.py", line 5, in inner_function
    print(x)  # Trying to access 'x' from the outer function
UnboundLocalError: local variable 'x' referenced before assignment

Global Variable Modification

In this code, the function example_function tries to increment the global variable ‘x’, but encounters an UnboundLocalError since it’s treated as a local variable due to the assignment operation within the function.

Python3
x = 5  # Global variable
def example_function():
    x += 1  
    print(x)

example_function()

Output

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 7, in <module>
    example_function()
  File "Solution.py", line 4, in example_function
    x += 1  # Trying to modify global variable 'x' without declaring it as global
UnboundLocalError: local variable 'x' referenced before assignment

Solution for Local variable Referenced Before Assignment in Python

Below, are the approaches to solve “Local variable Referenced Before Assignment”.

  • Global Variable Modification
  • Nested Function Variable Access

Global Variable Modification

In this code, example_function successfully modifies the global variable ‘x’ by declaring it as global within the function, incrementing its value by 1, and then printing the updated value.

Python3
x = 5 
def example_function():
    global x  # Declare 'x' as global
    x += 1    # Modify global variable 'x'
    print(x)

example_function()

Output
6

Nested Function Variable Access

In this code, the outer_function defines a local variable ‘x’, and the inner_function accesses and modifies it as a nonlocal variable, allowing changes to the outer function’s scope from within the inner function.

Python3
def outer_function():
    x = 10  

    def inner_function():
        nonlocal x  # Declare 'x' as nonlocal
        print(x)    # Access 'x' from the outer function

        x = 20      # Modify outer function's local variable 'x'
        print(x)

    inner_function()

outer_function()

Output
10
20

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads