Module 9 · Functions — topic 14 of 19
Global variables
Module-level names, reading them from functions, changing them with the global statement, and why shared mutable state should be kept to a minimum.
Learning objectives
- Read global constants from functions
- Use the global statement correctly when necessary
- Explain the drawbacks of mutable global state
Definition
A global variable is a name defined at the top level of a module. Functions can read globals directly; to rebind a global from inside a function, the function must declare it with the global statement.
Introduction
Values like TAX_RATE = 0.18 or MAX_USERS = 100 are naturally global: many functions use them and they don't change. Mutable global state, however, quickly makes programs hard to follow.
Why it matters
Knowing when globals are fine (constants, configuration) and when they hurt (counters, caches modified from everywhere) is an important design skill.
Detailed explanation
A function can read any global name that it doesn't also assign. Reading works because Python looks in the local scope first, then the global scope.
To rebind a global inside a function, declare it: global counter. Without the declaration, assignment creates a local.
Mutating a global object (appending to a global list) doesn't need global, because it doesn't rebind the name — but it is still a hidden side effect.
Globals make functions depend on invisible state: their behaviour changes depending on what other code did earlier, which makes testing and debugging harder.
Convention: constants are written in UPPER_CASE at the top of a module and never reassigned.
Syntax
- Reading a global needs no declaration.
- Rebinding needs global.
- Prefer constants over mutable globals.
Code examples
Reading a global constant
295.0
The global statement
3
More examples
Replacing hidden state with explicit state
global: 150 explicit: 150
Step-by-step explanation
- The first version changes a module-level balance through global — any function anywhere could change it too.
- The second version receives the current value and returns the new one; its result depends only on its arguments.
- Pure functions like deposit_to are easy to test: deposit_to(0, 100) is always 100.
Expected output
Running “Replacing hidden state with explicit state” prints exactly:
Practical applications
- Configuration constants and lookup tables are typical globals.
- Larger programs hold state in objects (classes) rather than globals.
Best practices
- Use globals for constants only, in UPPER_CASE.
- Pass state as parameters and return new values.
- Group related changing state into objects.
Common mistakes
Assigning without global
Avoid
Prefer
Without global, count is local and read before assignment: UnboundLocalError.
Overusing global
Avoid
Prefer
Hidden shared state makes bugs depend on call order.
Important notes
- global applies to module-level names; for an enclosing function's variable use nonlocal (see Scope).
Key takeaways
- Functions can read globals freely.
- Rebinding a global requires the global statement.
- Mutable global state makes code hard to test; prefer explicit state.
Try it yourself
- 1Convert a program that keeps a high score in a global into one that passes it around.
- 2Define three UPPER_CASE constants and use them in two functions.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?