Linked Questions
454 questions linked to/from UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
392
votes
5
answers
1.5m
views
How can I fix "UnboundLocalError: local variable referenced before assignment"? [duplicate]
The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment:
Var1 = 1
Var2 = 0
def function():
if Var2 == 0 and Var1 > 0:
print("...
231
votes
8
answers
334k
views
Why does this UnboundLocalError occur (closure)? [duplicate]
What am I doing wrong here?
counter = 0
def increment():
counter += 1
increment()
The above code throws an UnboundLocalError.
165
votes
3
answers
494k
views
Local (?) variable referenced before assignment [duplicate]
test1 = 0
def test_func():
test1 += 1
test_func()
I am receiving the following error:
UnboundLocalError: local variable 'test1' referenced before assignment.
Error says that 'test1' is local ...
84
votes
5
answers
419k
views
Local variable referenced before assignment? [duplicate]
I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime a URL is processed and ...
39
votes
4
answers
64k
views
Why can't I set a global variable in Python? [duplicate]
How do global variables work in Python? I know global variables are evil, I'm just experimenting.
This does not work in python:
G = None
def foo():
if G is None:
G = 1
foo()
I get an ...
user avatar
slim
77
votes
3
answers
580k
views
"cannot access local variable 'a' where it is not associated with a value", but the value is defined [duplicate]
I don't know why when a is located in def test() it can not be found and gives the error
UnboundLocalError: cannot access local variable 'a' where it is not associated with a value
Code
import ...
52
votes
3
answers
64k
views
Local functions in Python [duplicate]
In the following Python code, I get an UnboundLocalError. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I ...
41
votes
4
answers
20k
views
Python scope: "UnboundLocalError: local variable 'c' referenced before assignment" [duplicate]
I am trying to figure out this:
c = 1
def f(n):
print c + n
def g(n):
c = c + n
f(1) # => 2
g(1) # => UnboundLocalError: local variable 'c' referenced before assignment
Thanks!
33
votes
8
answers
55k
views
How can I change a global variable from within a function? [duplicate]
I'm trying to add or subtract from a defined variable, but how can I overwrite the old value with the new one?
a = 15
def test():
a = a + 10
print(a)
test()
Error message:
Traceback (most ...
33
votes
7
answers
8k
views
Why can functions in Python print variables in enclosing scope but cannot use them in assignment? [duplicate]
If I run the following code:
x = 1
class Incr:
print(x)
x = x + 1
print(x)
print(x)
It prints:
1
2
1
Okay no problems, that's exactly what I expected. And if I do the following:
x = 1
...
34
votes
2
answers
38k
views
Accessing variables defined in enclosing scope [duplicate]
From the Google Style Guide on lexical scoping:
A nested Python function can refer to variables defined in enclosing
functions, but can not assign to them.
Both of these seem to check out at first:...
19
votes
3
answers
4k
views
Python: LOAD_FAST vs. LOAD_DEREF with inplace addition [duplicate]
Last Friday I went to a job interview and had to answer the following question: why does this code raise an exception (UnboundLocalError: local variable 'var' referenced before assignment on the line ...
2
votes
5
answers
79k
views
Why am I getting a local variable referenced before assignment error? [duplicate]
The answer should be 2 because first the main() function is called, then the first() function is called, overriding the global variable num = 0 which was defined outside of any functions, therefore ...
1
vote
2
answers
62k
views
'str' object has no attribute 'len' [duplicate]
Ive got a method that use to work by checking the first three letters/numbers and making sure they are the same before it continues like so
def combineProcess(request):
carID1 = request.POST['...
11
votes
1
answer
43k
views
Python: UnboundLocalError: local variable 'count' referenced before assignment [duplicate]
I cannot understand what is the problem in my Python code. It gives me the following error:
Traceback (most recent call last):
File "main.py", line 77, in <module>
main();
File "...