Python UnboundLocalError: cannot access local variable before assignment

Function assigns to a variable name somewhere in its body, shadowing an outer variable.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.0.0
Identifier: Python UnboundLocalError: cannot access local variable before assignment Platform: Python

Interactive Diagnostic Checklist

Safe Diagnostic Command:
python3 -m pyflakes script.py

Technical Explanation

Function assigns to a variable name somewhere in its body, shadowing an outer variable. Verified technical resolution: Pass state as explicit parameters/return values rather than mutating globals.

Worked Examples

  • Diagnostic check: python3 -m pyflakes script.py

Limitations & Assumptions

  • Confirm exact software version and execution layer before applying configuration changes in production.

Source References & Verification

Resource ID: err-python-unboundlocalerror · Independently verified: 2026-11-25

Related Resources