The built-in globals() function returns the dictionary that holds the global namespace of the current module. Every module-level name (variables, functions, classes, imports) is a key in that dictionary, and the value is the object it refers to. The dictionary is live, not a copy: reading it shows the current globals, and writing to it creates, changes or removes global names.
Use globals() for inspection, debugging and name-based lookups. For ordinary code, the global statement or a plain dictionary you own is almost always the clearer choice.
globals() returnsglobals() compared with locals() and vars()globals() returnsglobals() takes no arguments and returns the __dict__ of the module in which the calling function was defined. Because it is the real namespace, an assignment through the dictionary is visible as a normal variable in that module.
Besides your own names, the dictionary always contains entries such as __name__, __doc__, __file__ (for modules loaded from a file) and __builtins__. A new script therefore never has an empty globals().
Reading is a normal dictionary lookup. Writing works at module level and inside functions, because the dictionary belongs to the module and not to the function.
For this simple case the global statement is the idiomatic spelling and is easier for readers, linters and type checkers to follow:
You can create a global whose name is only known at run time. This is occasionally useful in small scripts, REPL helpers and code generators.
Static analysis tools cannot see names created this way, so editors will flag MAX_RETRIES as undefined and autocomplete will not find it. If you need a set of named values, a normal dict, an Enum or a dataclass is safer.
A common legitimate use is dispatching to a function whose name arrives as a string, for example from a command-line argument or a config file.
This exposes every callable in the module, including helpers you never meant to be commands. An explicit table is safer and just as short:
Never pass untrusted input to globals()[name]() or to eval() and exec(): the caller then controls which of your functions run.
Iterating over globals() is a quick way to see what a module defines. Filter out dunder names and imported modules to keep the output readable.
If you iterate while code might add names, copy first with list(globals().items()). Adding a key during iteration raises RuntimeError: dictionary changed size during iteration.
globals() compared with locals() and vars()| Function | Returns | Writable |
|---|---|---|
globals() | The module's global namespace dictionary | Yes, changes take effect |
locals() | The current local namespace | In a function, no: changes do not affect variables |
vars(obj) | The __dict__ of obj; without an argument it behaves like locals() | Depends on the object |
At module level globals() and locals() return the same dictionary. Inside a function they differ: locals() gives a snapshot of local variables. Since Python 3.13 (PEP 667), locals() in a function returns an independent snapshot on every call, which makes the "do not write to it" rule explicit.
globals() is also the namespace argument used by exec() and eval(). Passing globals() shares your module state with the executed code; pass a fresh {} when you want isolation, and remember that this is not a security sandbox.
globals() return a copy of the global variables?No. It returns the module's real namespace dictionary, so changes through it affect the module. Copy it with dict(globals()) when you need a snapshot.
globals() the same in every module?No. Each module has its own global namespace. A function returns the globals of the module where it was defined, not of the module that called it. To inspect another module use vars(module) or module.__dict__.
globals() instead of the global keyword?Rarely. Use global when a function must rebind a module variable, and use globals() only when the name is not known until run time.
globals()?Yes. del globals()["name"] removes the name, and later use raises NameError. Do this only in tooling code, not in application logic.
globals() gives you live access to the module namespace as a dictionary. It is useful for inspection, REPL tooling and name-based lookups, and it can create or remove globals at run time. Prefer the global statement for rebinding variables, explicit dictionaries for dispatch tables, and never expose globals() to untrusted input.