In this post, we will see about error Local variable referenced before assignment
in Python.
Table of Contents
Problem
Let me explain this error with the help of an example.
1 2 3 4 5 6 7 |
a = 100 def incrementA(): a=a+5 return a print("value of a:",incrementA()) |
When you run above program, you will get below output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-89-c87da0a8ced6> in <module> 3 a=a+5 4 return a ----> 5 print("value of a:",incrementA()) <ipython-input-89-c87da0a8ced6> in incrementA() 1 a = 100 2 def incrementA(): ----> 3 a=a+5 4 return a 5 print("value of a:",incrementA()) UnboundLocalError: local variable 'a' referenced before assignment |
Let’s understand why we are getting this error UnboundLocalError
.
When Python interpreter parses any function definition and finds any assignment such
1 2 3 |
a = |
Python interpreter treats a
as local variable by default.
Since a
is considered as local variable, when python executes
1 2 3 |
a=a+5 |
it evaluates right-hand side first and tries to find value of variable a
. It finds a
first time, hence raised this error: UnboundLocalError: local variable referenced before assignment
Solution
Using global
To resolve this, you need to explicitly declare a variable as global and it will resolve this issue.
Let’s implement this solution in our program.
1 2 3 4 5 6 7 8 |
a = 100 def incrementA(): global a a=a+5 return a print("value of a:",incrementA()) |
When you run above program, you will get below output:
As you can see, after declaring global a
in incrementA()
function, we solved this issue.
💡 Did you know?
Please note that if you don’t use assignment with variable, you won’t get this error.
123456 a = 100def incrementA():return aprint("value of a:",incrementA())When you run above program, you will get below output:
value of a: 100If you use direct assignment without using variable, then also you won’t get this error.
1234567 a = 100def incrementA():a=200return aprint("value of a:",incrementA())When you run above program, you will get below output:
value of a: 200
Using non-local
You can use nonlocal
to resolve this issue if you are using nested functions. nonlocal
works in nested functions and generally used when variable should not belong to inner function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
a = 100 def incrementA(): a = 200 def incrementABy10(): # will refer a=200 nonlocal a a=a+10 print("value of a inside function:",a) incrementABy10() a=a+5 return a print("value of a:",incrementA()) |
Output:
value of a: 215
When we use nonlocal
, it refers value from nearest enclosing scope.
Let’s change nonlocal
to global
and you will be able to understand the difference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
a = 100 def incrementA(): a = 200 def incrementABy10(): # will refer a=100 global a a=a+10 print("value of a inside function:",a) incrementABy10() # will refer a as 200 a=a+5 return a print("value of a:",incrementA()) |
Output:
value of a: 205
As you can see, value of a
inside incrementABy10()
was 100 due to global a
, then it was incremented by 10 and when we used a
inside incrementA()
, it was 200 as a
is considered local variable here and then incremented it by 5.
That’s all about local variable referenced before assignment in Python.