What this thread does
UnboundLocalError: cannot access local variable 'result' where it is not associated with a value
A Python function reads a local name before that invocation has bound it to a value.
First action — place breakpoint() immediately before the failing read and inspect the function's assignments and the branch that ran.
The official FAQ establishes assignment-driven shadowing and outer-scope declarations; the application's control flow is untested, and no general NameError fix is established here.
Not for: Python cannot import name from a partially initialized module; see python-circular-import-cannot-import-name-partially-initialized-module.
How to diagnose and fix
1. Read the whole function for assignments to the failing name, including +=. The official programming FAQ explains that assignment makes a name local and can shadow an outer name even before that assignment executes. 2. Decide which scope should own the value. If the function intentionally updates a module-level binding, the FAQ demonstrates global. If a nested function intentionally updates its enclosing function's binding, it demonstrates nonlocal. 3. If the value should be local, inspect which branch must assign it before the read. Do not add global merely because the exception mentions a local name. Any control-flow repair must follow the function's intended behavior. 4. If the exception is instead NameError: name 'spam' is not defined, follow the missing name and traceback separately. The official errors tutorial gives that concrete example, but supplies no general NameError repair recipe.
The following illustrates the FAQ's module-binding case, with the variable renamed:
result = 10
def broken():
print(result)
result += 1
def update_outer():
global result
print(result)
result += 1
Use update_outer only if changing the shared binding is the intended contract. For a nested binding, put nonlocal result inside the nested function that updates it, after ensuring the enclosing function defines that binding. See the FAQ's nested example.
Diagnostic: UnboundLocalError: cannot access local variable 'result' where it is not associated with a value
The CPython evaluator defines cannot access local variable '%s' where it is not associated with a value; substituting result gives the opening message. CPython 3.12 source checked 2026-10-28. The FAQ illustrates the same scope rule with older wording; use the source template for the exact message above.
What to test
Exercise every path reaching the read, including an exception before assignment. For an intentional outer update, verify both the returned behavior and the outer value after repeated calls. Test the nested function separately if using nonlocal.
Pitfalls
- Adding a shared global binding to conceal a missed local assignment.
- Initializing to
Nonewithout deciding whether downstream code accepts it. - Treating UnboundLocalError and NameError as interchangeable diagnoses.
Public evidence
Sudhanshu-Badole's Frappe issue #39506, opened 2026-05-26 (checked 2026-10-28), reports an unset doc after an exception: “doc is never assigned”. The proposed fix moves its permission check inside the try block; contributor sumitjain236 confirms the change in linked PR #39527. This is evidence of a control-flow case, not evidence that global fixes every occurrence.
A prompt to give your agent
Read the linked official FAQ and evaluator source; your training may predate the exact wording, so do not guess from the error alone. Show every binding and read of the failing name and trace the branch that reaches it. Use global or nonlocal only for intended outer mutation. Distinguish a separate NameError and state when its cause or fix is not established. Propose tests for the missed branch.
Limits
The documented declaration fix covers outer-name shadowing only. The public report covers one exceptional control-flow path. A general NameError cause or fix is not established by these examples. No application repair or sample execution has been tested here; older interpreter message variants are outside scope.
Started from a post by @Sudhanshu-Badole, packaged by the editor. Original post