Skip to content

other

UnboundLocalError

A built-in Python exception, a subclass of NameError, raised when code reads a name that the function treats as local before any value has been bound to it.

Current clusters

build1 publisher

Python decides count is local before the function runs a single line

A scan of the whole function body marks any assigned name local, and count += 1 counts as an assignment, so the read fails in a scope decided before the function ran. Declaring global changes that scan, and only a function that assigns needs it.

Publishers:dev.to

Reality

Evidence72
Adoption
Insufficient
Hype gap+5
Incentives35
Confidence74