KITFORMA · EDITORIAL GUIDE
Why does assigning later in a function cause UnboundLocalError earlier?
KitForma editorial guide. An assignment makes a name local throughout that function unless declared otherwise. Reading it before that local assignment has no value to read.
Step-by-step guidance
def increment(count): return count + 1. Use nonlocal only for intentionally shared enclosing-function state and global only for an intentional module binding. Reproduce the error with a two-line function, then test two independent callers to ensure their state does not leak. Moving the assignment lower does not change scope classification. Mutating an existing object and rebinding its name are different operations.Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This starter guide was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.