Skip to content

Topic

Python variable scope

The rules that decide whether a name in Python code refers to a local, enclosing, module-level or built-in binding, including the compile-time decision that treats any assigned name as local to its function.

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