Python 3 reference · Built-in Functions
Python globals() Function
Return the dictionary of the current module's global names: variables, functions, classes and imports.
Python 3 Since 3.0
Syntax
globals()
Return value
The module's global namespace as a dict (changes to it affect the module).
Examples
app_name = "demo"
def helper(): pass
g = globals()
print("app_name" in g, g["app_name"])
print(callable(g["helper"]))
Output
True demo True
def greet(): return "hi"
def bye(): return "bye"
for name in ["greet", "bye"]:
print(globals()[name]())
Output
hi bye
Good to know
Prefer a normal dictionary for "lookup by name" tables (actions = {"greet": greet}) instead of reaching into
globals(); it is clearer and safer.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
locals()· Return a dictionary of the local variables in the current scope. Inside a function it is a snapshot since Python 3.13.vars()· Return the __dict__ of a module, class or object: its writable attributes as a dictionary. With no argument, like locals().dir()· List the names an object has, such as attributes and methods. With no argument, list the names in the current scope.- Python Scope · tutorial