Python UnboundLocalError
It could be surprising to see when you add assignment statement in the
code and you started to get this UnboundLocalError
error case.
Lets see how does Python internally handles this. Since, Python is Interpreted language this error has lot to do with how it identifies the scope of the variable. When python evaluates the particular block, it would idenify the scope of the variable by scanning the entire block.
UnboundLocalError Explained
UnboundLocalError: local variable 'x' referenced before assignment
First we will understand what is this UnboundLocalError
|
|
Now, lets add the assignment operator in this same code.
|
|
When we add line:4 x+=1
assignment operator in a scope, that variable becomes local to that
scope and shadows the outer scope's same name. As a result of this, in line number 3 x
is printed before its assignment
operator. Hence, python is giving UnboundLocalError: local variable 'x' referenced before assignment
Solution
|
|
By adding, global
scope in line number 3, Python takes x
as global scope and fixes this issue.
Rules for Scopes
- Variables that are only referenced
(E.g. print(x))
inside a function are implicitly global - If a variable is assigned a value
(E.g. x += 1)
anywhere within the function's body, it's assumed to be a local unless explicitly declared as global.
Actually executing with this example would give better perspective of how python handles its scopes. I will be start exploring Python's internal magic and it would be key learning process.