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

Look up global namesTry it yourself
app_name = "demo"
def helper(): pass

g = globals()
print("app_name" in g, g["app_name"])
print(callable(g["helper"]))

Output

True demo
True
Find a function by nameTry it yourself
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

VersionChange
Python 3.0Added