Python

Python UnboundLocalError

Python’s UnboundLocalError seems very confusing when newcomers to Python experience it for the first time. However, it is quite easy. An UnboundLocalError can occur due to two reasons in Python, one is when you are trying to access the value of a variable before it is assigned, and the second is when Python fails to differentiate between global and local variables.

This post will act as your guide to solve Python’s UnboundLocalError with possible solutions.

Reason 1: Accessing Local Variable Before Assigning Value

Normally, when the programmer is not working inside any functions or local scope variables, trying to access a variable before initializing it with a value returns the following error:

However, when the same mistake is performed with local variables, say inside a function, then the program returns the UnboundLocalError, meaning that it couldn’t find the local variable while accessing it. To demonstrate this, the following code:

def func():
    print(x)
    x = 420

func()

 

Executed it will produce the following result:

Solution: Fixing Access Order of Local Variable

To avoid encountering this error, simply make sure that you are initializing the local variable with some value before trying to access it. The correct code for the above example is as follows:

def func():
    x = 420
    print(x)
   
func()

 

The variable “x” is assigned a value before it is accessed, and now when this program is executed it produces the following result on the terminal:

The output verifies that you have successfully fixed the UnboundLocalError caused by the wrong order of accessing the local variable.

Reason 2: Python Cannot Differentiate Between Local and Global Variables

This reason is a little confusing for most new Python enthusiasts and nothing is logically wrong in the program. To demonstrate this error in a non-confusing way, let’s take the following code for accessing a global variable inside a function:

x = 420
def func():
    print(x)
   
func()

 

This code produces the error-free output which is as follows:

But when some assignment is made to the global variable inside the function, such as:

x = 420
def func():
    print(x)
    x= 33
func()

 

It starts producing the UnboundLocalError as shown below:

The reason for this is that as soon as a change in the global variable is made, the program thinks the user wants to access and change a local variable, due to which it fails to find that local variable and returns the UnboundLocalError.

Solution: Use the global Keyword to Solve Confusion

The best way to solve this issue is to simply use the keyword “global” at the start of the function to tell the program that this local variable is linked with the global variable. To demonstrate this solution, take the following code:

x = 420
def func():
    global x
    print(x)
    x= 30
func()

 

This will produce the following output on the terminal when executed:

You have successfully learned the reasons for the UnboundLocalError along with its solution.

Conclusion

The UnboundLocalError can be caused by trying to access a local variable before assigning it some value, or otherwise, it can be caused when the program cannot differentiate whether the user wants to access the local variable or the global variable. Both of these reasons have been explained along with their demonstration and solution thoroughly in this post.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.