Local variable referenced before assignment in Python

In Python, while working with functions, you can encounter various types of errors. A common error when working with the functions is “Local variable referenced before assignment”. The stated error occurs when a local variable is referenced before being assigned any value.

This write-up will provide the possible reasons and the appropriate solutions to the error “Local variable referenced before assignment” with practical examples. The following aspects are discussed in this write-up in detail:

Reason: Reference a Local Variable

The main reason for the “local variable referenced before assignment” error in Python is using a variable that does not have local scope. This also means referencing a local variable without assigning it a value in a function.

The variable initialized inside the function will only be accessed inside the function, and these variables are known as local variables. To use variables in the entire program, variables must be initialized globally. The below example illustrates how the “UnboundLocalError” occurs in Python.

Output:

In the above snippet, the “Student” variable is not marked as global, so when it is accessed inside the function, the Python interpreter returns an error.

Note: We can access the outer variable inside the function, but when the new value is assigned to a variable, the “UnboundLocalError” appears on the screen.

Solution 1: Mark the Variable globally

To solve this error, we must mark the “Student” variable as a global variable using the keyword “global” inside the function.

Within the function, we can assign a new value to the student variable without any error. Let’s have a look at the below snippet for a detailed understanding:

Code:

Student = 'Anna'

def names():
   
    global Student
   
    print(Student)
   
    Student = 'Lily'

names()
names()

In the above code, the local variable is marked as a “global” variable inside the function. We can easily reference the variable before assigning the variable in the program.

Output:

The above snippet proves that the global keyword resolves the “unboundLocalError”.

Solution 2: Using Function Parameter Value

Passing a value as an argument to the function will also resolve the stated error. The function accepts the variable as an argument and uses the argument value inside the function. Let’s have a look at the given below code block for a better understanding:

Code:

def names(students):
   
    print(students)
    students = 'Lily'
    print(students)

names('Alex')

In the above code, the variable is referenced before assigning the value inside the user-defined function. The program executes successfully without any errors because the variable is passed as a parameter value of the function.

Output:

The above output shows the value of the function when the function is accessed in the program without any “Local Variable referenced” error.

Solution 3: Using nonlocal Keyword

The “nonlocal” keyword is utilized in the program to assign a new value to a local variable of function in the nested function. Here is an example of code:

Code:

def students():
    name = 'Lily'
   
    def marks():
        nonlocal name
        print(name)
        name = 'Alex'
        print(name)
    marks()

students()

In the above code, the keyword “nonlocal” is used to mark the local variable of the outer function as nonlocal. After making the variable nonlocal, we can reference it before assigning a value without any error.

Output:

The above output shows the value of the inner function without any “local variable referenced” error in a program.

Conclusion

The “Local variable referenced before assignment” appears in Python due to assigning a value to a variable that does not have a local scope. To fix this error, the global keyword, return statement, and nonlocal nested function is used in Python script. The global keywords are used with variables to make it able to access inside and outside the function. The return statement is also used to return the variable’s new value back to function and display the result on the screen. This Python guide presented a detailed overview of the reason and solutions for the error “Local variable referenced before assignment” in Python.