How To Fix Unbound Local Errors In Python
Written by
The beginner users of python may get confused when they see an Unbound Local Error problem in Python. To save them from this headache we have provided the cause, and the fixes of this error, which often occurs while coding in Python.
How UnboundLocalError: local variable referenced before assignment
occurs
UnboundLocalError: local variable referenced before assignment
, error occurs when we are trying to reference the variable inside a function before assigning any value to it.
This error occurs only when the local variable which is in focus is being assigned some value in the function, and we have referenced it before the assignment operation.
Let us take the help of the code to see what I am trying to say:-
Example 1
string = "abcd"
def example():
string = string + "efgh"
print(string)
example()
The above example will show an error in line 3, where it says UnboundLocalError: local_variable 'string' referenced before assignment
.
The above error occurs because there is both assignment and reference of the variable string, and thus, program creates a local variable named as string for the function example, but it is referenced before any value has been assigned(in the line 3, as we are trying to use the value of string to update the value of string) and since, till then there is no value for local variable string in the function, it shows unbound local error.
Had the code been, as given below:-
Example 2
string = "abcd"
def example():
#string = string + "efgh"
print(string)
example()
It would have simply displayed the output:-
abcd
As in this case, there is no assignment of the value to the string variable, and thus, no local variable string
is created, and thus, it uses the value in the global scope to display.
The problem of UnboundLocalError
occurs only when there is an assignment operation of the variable having the same name as outside the function and inside the function, and the variable is referenced before the assignment operation has taken place.
How to Fix UnboundLocalError
error
Only way to avoid this error is to assign the variable before referencing it, or to modify it inside the function with a different name and then return that modified value and assign it to the variable outside the function.
Various Ways of Fixing the Above Error
Sometimes we need to use the value of the variable outside the function, and thus it may require referencing before assignment.
There are some ways by which we can bypass this problem, where we need to reference before assignment.
- Using
global
keyword:-
string = "abcd"
def example():
global string
print(string)
string = "efgh"
print(string)
example()
The output of the above code is:-
abcd
efgh
Using the global
keyword we can easily circumvent the problem of no referencing before assignment, as then it would be able to use the global variable for further operations inside the function.
- We can pass the value of variable outside the function as a parameter to the function:-
string = "abcd"
def example(string):
print(string)
string = "efgh"
print(string)
example()
The output of the above code is:-
abcd
efgh
Upon passing the value, we will be able to use the variable’s value without declaring it as a global variable, as sometimes it is beneficial to not expose the value to every function.
- Sometimes we want to modify the value of global variable without declaring it as a global variable, we can simply use some other variable, and modify it accordingly and then return its value and assign it to variable whose value we wanted to modify, like in the given example:-
string = "abcd"
def example():
string__ = "efgh"
return string__
string = example()
print(string)
The output of the above code will be :-
efgh
In the above code, we have modified the variable string without declaring it as a global variable, by returning the modified value from the function.
- If we are using a nested function and we want to assign a value to the local variables from the outer function, then we can use the “nonlocal” keyword to access that variable inside the nested function.
def outer_example():
#initializing string variable
string = ' '
def inner_example():
Marking string as nonlocal
nonlocal string
string = 'hello world'
print(string)
inner_example()
print(string)
outer_example()
The output of the above function will be:-
hello world
hello world
We can see in the output that we have modified the string present in the outer_example()
by accessing it inside the inner_example()
.
We can also use the methods numbered 2 and 3 along with this one to modify the variable present in the outer function from the inner function.
Conclusion
We just saw the reasons behind the occurrence of Unbound Local Error in Python, along with various fixes which we can use in our function to bypass the problem along with getting our task accomplished.