fuckingpython.lol

Python errors › UnboundLocalError

UnboundLocalError: cannot access local variable where it is not associated with a value

Updated

The Python error "cannot access local variable … where it is not associated with a value", once "referenced before assignment": why it happens, and the fix.

What you see

main.py
count = 0

def bump():
    count += 1
    return count

print(bump())
Python 3.15 prints
Traceback (most recent call last):
  File "/home/me/project/main.py", line 7, in <module>
    print(bump())
          ~~~~^^
  File "/home/me/project/main.py", line 4, in bump
    count += 1
    ^^^^^
UnboundLocalError: cannot access local variable 'count' where it is not associated with a value

What it means

If a function assigns to a name anywhere in its body, that name is local to the whole function, from its first line. count += 1 is an assignment, so inside bump the name count is local, and it is read before anything was put in it. The global count = 0 is not consulted at all.

Python decides this when it compiles the function, not line by line as it runs. That is why a read on the first line can fail because of an assignment further down.

Until Python 3.10 the same error read "local variable 'count' referenced before assignment". Python 3.11 changed the wording; the cause is the same.

Why it happens

  • Changing a global or an outer variable with +=, -= or = inside a function, as in the example.
  • Reading a name before a later assignment in the same function: print(x) and then x = 2 a few lines down.
  • Assigning in only one branch. if ok: result = 1, then return result when ok was false.
  • An except or for that did not run, so the name it would have bound was never set.

How to fix it

The cleanest fix is to pass the value in and return the new one:

def bump(count):
    return count + 1

count = 0
count = bump(count)

If the function really should change a module-level variable, declare it with global. For a variable of an enclosing function, use nonlocal:

count = 0

def bump():
    global count
    count += 1
    return count

For the one-branch case, give the name a value before the if.

How each Python version words it

PythonPrints
3.9–3.10UnboundLocalError: local variable 'count' referenced before assignment
3.11–3.15UnboundLocalError: cannot access local variable 'count' where it is not associated with a value

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.

3.9 and 3.10 print the old wording, "local variable 'count' referenced before assignment". 3.11 and later print the new one. Searching for either finds the same cause.

Questions

Why is it not just a NameError?

It is a kind of one: UnboundLocalError is a subclass of NameError. The name exists as a local, it just has no value yet, and the separate class says so.

Why does reading a global work but += does not?

Reading alone does not make a name local, so Python finds the global. Any assignment, including +=, makes it local for the whole function.

Sources

Read on October 25, 2026.