Sub topic 4
Global names at module level
This chapter assumes you can define and call functions, read a simple traceback, and know that assignment binds a name to an object. It covers what "global" means in Python, how functions read global names, and where those names are stored.
What makes a name global
A name is global when you bind it at the top level of a module, outside any function or class. Python stores these names in the module's global namespace, which is a dictionary.
A function can read a global name it never assigns. Lookup starts in the function's local scope, finds nothing, and falls through to the global scope.
count = 0
def read_count():
return count
print(read_count())
# Output: 0Here count is never assigned inside read_count, so Python looks it up in the module's globals.
Global means module-wide
"Global" in Python does not mean program-wide. Each module (each .py file) has its own global namespace. If a.py and b.py both define count, they are two unrelated names.
When you write import math, Python binds the name math in your module's global namespace. Imported names, function names, and class names all become globals this way, so def and import at the top level create global names just as = does.
| Statement at module level | Global name created |
|---|---|
| `x = 5` | `x` |
| `def f(): ...` | `f` |
| `class C: ...` | `C` |
| `import math` | `math` |
Lookup happens at call time
Python does not resolve a global name when it compiles the function. It resolves the name each time the function runs. The global only has to exist by then.
def greet():
return PREFIX + ", world"
PREFIX = "hello"
print(greet())
# Output: hello, worldgreet is defined before PREFIX, but this works because greet() is called after PREFIX is bound. If you called greet() first, you would get a NameError.
The same rule explains why functions can call each other regardless of the order in which they are defined, as long as both exist when the call happens.
Inspecting the global namespace
The built-in globals() returns the current module's global namespace as a dictionary. It is useful for seeing what Python has bound.
limit = 5
print("limit" in globals())
print(globals()["limit"])
# Output:
# True
# 5Treat this as a debugging aid. Writing to the dictionary does change the module's names, but code that does so is hard to follow.
The diagram leaves out enclosing function scopes. A later chapter on the LEGB rule adds them.
Common errors
- Assuming reads and writes behave the same. Reading a global from a function works. Assigning to the same name inside the function creates a new local name instead. The next chapters explain this and how to change it.
- Expecting globals to cross modules. Setting
count = 1in one file does not changecountin another file that imported it withfrom other import count. That statement copied the binding into the importing module's namespace. - Calling a function too early. A function that reads a global fails with
NameErrorif the call runs before the global is bound.
Exercise
What does this code print, and why? total = 10 def show(): return total total = 20 print(show())
Show answer
It prints 20. The function does not assign total, so it looks the name up in the module's globals each time it runs. By the time show() is called, total has been rebound to 20.
Exercise
Module a.py defines value = 1. Module b.py runs from a import value and then value = 2. What is a.value afterward?
Show answer
It is still 1. The import copied the binding into b's global namespace, and the later assignment rebinds only b's own name value.