Python 3: UnboundLocalError: local variable specified before assignment

The following code gives an UnboundLocalError: local variable 'Var1' referenced before assignment error UnboundLocalError: local variable 'Var1' referenced before assignment :

 Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print("Result One") elif Var2 == 1 and Var1 > 0: print("Result Two") elif Var1 < 1: print("Result Three") Var1 =- 1 function() 

How can i fix this? Thanks for any help!

+69
function python
Jun 01 2018-12-12T00:
source share
5 answers

You can fix this by passing parameters rather than relying on Globals

 def function(Var1, Var2): if Var2 == 0 and Var1 > 0: print("Result One") elif Var2 == 1 and Var1 > 0: print("Result Two") elif Var1 < 1: print("Result Three") return Var1 - 1 function(1, 1) 
+19
Jun 01 2018-12-12T00:
source share

This is because although Var1 exists, you also use the Var1 operator Var1 inside the function ( Var1 -= 1 on the bottom line). Naturally, this creates a variable inside the scope of the Var1 function (though, -= or += will only update (reassign) an existing variable, but for unknown reasons (likely consistency in this context) Python considers it to be a task). The Python interpreter sees this during module loading and correctly decides that the global region Var1 should not be used inside the local region, which leads to a problem when you try to refer to a variable before its local destination.

Using global variables, unless necessary, is usually underestimated by Python developers because this leads to confusing and problematic code. However, if you want to use them to execute your code, you can simply add:

 global Var1, Var2 

inside the top of your function. This will tell Python that you are not going to define the variable Var1 or Var2 inside the local function scope. The Python interpreter sees this during module loading and decides (correctly) to look for any references to the above variables in the global scope.

+171
Jun 01 2018-12-12T00:
source share

If you set the value of a variable inside a function, python understands it as creating a local variable with that name. This local variable masks the global variable.

In your case, Var1 considered as a local variable and is used before setting it, thus, an error.

To solve this problem, you can explicitly say its global by inserting global Var1 into your function.

 Var1 = 1 Var2 = 0 def function(): global Var1 if Var2 == 0 and Var1 > 0: print("Result One") elif Var2 == 1 and Var1 > 0: print("Result Two") elif Var1 < 1: print("Result Three") Var1 =- 1 function() 
+40
Jun 01 2018-12-12T00:
source share

I don't like this behavior, but this is how Python works. There were other answers to the question, but for completeness, let me point out that Python 2 has more of such quirks.

 def f(x): return x def main(): print f(3) if (True): print [f for f in [1, 2, 3]] main() 

Python 2.7.6 returns an error:

 Traceback (most recent call last): File "weird.py", line 9, in <module> main() File "weird.py", line 5, in main print f(3) UnboundLocalError: local variable 'f' referenced before assignment 

Python sees that f used as a local variable in [f for f in [1, 2, 3]] , and decides that it is also a local variable in f(3) . You can add a global f statement:

 def f(x): return x def main(): global f print f(3) if (True): print [f for f in [1, 2, 3]] main() 

It works; however, f becomes 3 at the end ... That is, print [f for f in [1, 2, 3]] now changes the global variable f to 3 , so it is no longer a function.

Fortunately, it works great in Python3 after adding parentheses to print .

+7
Oct 27 '14 at 1:10
source share

Why not just return the computed value and let the calling agent change the global variable. Manipulating a global variable inside a function is not recommended, as shown below:

 Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print("Result One") elif Var2 == 1 and Var1 > 0: print("Result Two") elif Var1 < 1: print("Result Three") return Var1 - 1 Var1 = function() 

or even create local copies of global variables and work with them and return results that the caller can then assign accordingly

 def function(): v1, v2 = Var1, Var2 # calculate using the local variables v1 & v2 return v1 - 1 Var1 = function() 
0
Feb 17 '14 at 18:39
source share



All Articles