local variable referenced before assignment python: UnboundLocalError

local variable referenced before assignment python: UnboundLocalError

Welcome everyone, Today we are going to learn what is exactly Unboundlocalerror and How to local variable referenced assign in python. so friends let’s start:

In Python code, variable that are only referenced inside a function are implicitly global methods. And if a variable is assigned any kind of value anywhere within the given function’s body, it is assumed to be a local unless explicitly declared as a global function.

This code:

>>> c = 20

>>> def bar():

...     print(c)

>>> bar()

20

It’s works, but this code:

>>> c = 20

>>> def foo():

...     print(c)

...     c += 1

Final results in an UnboundLocalError:

>>> foo()

Traceback:

  ...

local variable ‘x’ referenced before assignment: UnboundLocalError

If you need suddenly start getting UnboundLocalError in your code which was working perfectly just a few time ago. And after what you did is just added an assignment statement.

when the  print(x) attempt to print the uninitialized local variables and an get error results.

Example

>>> x = 20

>>> def foobar():

...     global x

...     print(x)

...     x += 1

>>> foobar()

20

This explicit declaration is also required :

>>> print(x)

21

In a nested scope you can do a similar thing by using the nonlocal keyword:

>>> def foo():

...    c = 20

...    def bar():

...        nonlocal c

...        print(c)

...        c += 1

...    bar()

...    print(c)

>>> foo()

20

21

In this article we are basically used following function:

1.nonlocal: when we need to assign any kind of value to nested scope variable then this function very useful.

2.global: when we need to assign any values to global object variable inside any function then this function very helpful.

Let’s see some more examples:

First lets say your code is working fine.

paga!sach:~$ python3

Python 3.5.2 (default, oct 22 2020, 12:13:04) 

[GCC 5.4.0 20200609] on linux

Type "credits" or "license" "help","copyright" for more information.

>>> x = 20

>>> def printx():

...     print(x)

... 

>>> printx()

20

>>> 

Now we are added an assignment statement inside print(x) function then you will start getting UnboundLocalError in your code.

>>> x = 20

>>> def printx():

...     print(x)

...     x = 22

... 

>>> printx()

Traceback :

  File "<stdin>", line 1, in <module>

  File "<stdin>", line 2, in printx

UnboundLocalError: local variable 'x' referenced before assignment in python

>>> 
What caused UnboundLocalError in python code?

In first example x is global reson is we are not just assigning any value to x variable inside the function and just referencing it to print.

In second example, we assigned the value 22 to x, hence x variable scope is local to function and we are tried to print them before assigning any value to x variable.

must read : python method vs function vs class

Summary:

In this article we saw the local variable referenced before assignment python:UnboundLocalError so any question about this article then ask me freely

Tags: UnboundLocalError,local variable referenced before assignment python

Thanks for reading! If you liked this article, then please comment, like and share.

I am the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Comment