Local variable referenced before assignment

In this post, we will see about error Local variable referenced before assignment in Python.

[toc]

Problem

Let me explain this error with the help of an example.

a = 100
def incrementA():
    a=a+5
    return a
print("value of a:",incrementA())

When you run above program, you will get below output:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
 in 
      3     a=a+5
      4     return a
----> 5 print("value of a:",incrementA())

 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

a =

Python interpreter treats a as local variable by default.

Since a is considered as local variable, when python executes

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.

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:

value of a: 105

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.

a = 100
def incrementA():
    return a
print("value of a:",incrementA())

When you run above program, you will get below output:

value of a: 100

If you use direct assignment without using variable, then also you won’t get this error.

a = 100
def incrementA():
    a=200
    return a
print("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.

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 inside function: 210
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.

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 inside function: 110
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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.