Search notes:
Python Exception: UnboundLocalError: local variable '…' referenced before assignment
The following simple script causes the Exception
UnboundLocalError
to be raised.
This is because
Python
assumes a
variable
to be local to a
Python functions
if this variable is being assigned to in that function.
a_global_var = 42 def increment_global_var(): a_global_var += 1 increment_global_var()
Github repository
about-Python
, path:
/statements/global/local-variable-referenced-before-assignment.py
In order to be able to assign a value to a global variable in a function, it must be declared as global using the
global
statement
.
Index