Python globals() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, globals() returns the dict that holds the current module's global names: variables, functions, classes, and imports defined at the top level. It is the real namespace, so writing to it creates or changes global variables.

Inside a function, it still returns the module's namespace, not the function's local variables; use locals() for those. Most code should use the global statement or, better, pass values explicitly instead of editing globals().

globals() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

It takes no arguments.

python
namespace = globals()
CallWhat it does
globals()["x"]Reads the global x.
"x" in globals()Is there a global named x?
globals()["x"] = 1Creates or changes the global x.
globals()["__name__"]'__main__' when run as a script.

The dict contains special names like __name__ and __builtins__, so print selected keys rather than the whole thing.

Library declarations
python
globals()

Parameters

globals() takes no arguments.

Return Value

Return ValueDescription
dictThe current module's global namespace (live, not a copy).

Python Examples

Example 1 — Inspect the Module Namespace

Check the type of globals() and read a few selected keys.

python
counter = 0
g = globals()

print(type(g).__name__)
print("counter" in g, g["counter"])
print(g["__name__"])

How It Works

  1. A plain dict. The global namespace is an ordinary dictionary.
  2. Top-level names. counter is defined at module level, so it is a key.
  3. Special names. __name__ is '__main__' when the file is run directly.
  4. Selected keys only. The full dict would also list __builtins__, imports, and more.

Example 2 — The global Statement vs Editing globals()

Update a global with the global keyword, reset it by name, and create a new global through the dict.

python
count = 0


def increment():
    global count
    count += 1


def reset_by_name(name):
    globals()[name] = 0


increment()
increment()
print(count)
reset_by_name("count")
print(count)
globals()["created"] = "made via globals()"
print(created)

How It Works

  1. global keyword. global count lets the function rebind the module-level variable.
  2. By name. globals()[name] = 0 changes whichever global the string names.
  3. Live dict. Assigning a new key creates the variable created.
  4. Prefer explicit code. These tricks work, but returning values is easier to read and test.

Example 3 — Find Functions by Name and Compare with locals()

Look up functions in the global namespace and see where a function's local variable lives.

python
def greet():
    return "hello"


def farewell():
    return "goodbye"


for name in ["greet", "farewell", "missing"]:
    func = globals().get(name)
    print(name, "->", func() if callable(func) else "not found")


def inner_scope():
    temp = 1
    return "temp" in locals(), "temp" in globals()


print(inner_scope())

How It Works

  1. Functions are globals. Each def at the top level adds a key to the namespace.
  2. Safe lookup. globals().get(name) returns None for unknown names, which callable() rejects.
  3. Local stays local. temp exists in locals() inside the function, not in globals().
  4. Allow-lists are safer. In real code, a dict like {"greet": greet} limits what can be called.

Frequently Asked Questions

The dictionary of the current module's global names. It is the live namespace, not a copy.
globals() is always the module namespace. locals() is the current local scope: the function's variables inside a function.
Yes, globals()["name"] = value creates a global, but it is usually clearer to assign directly.
When a function must rebind a module-level variable. Reading a global needs no keyword.
Every module namespace links to the built-ins, which is how names like len and print are found.

Next: hasattr()

Check whether an object has an attribute.

hasattr() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful