Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

globals()

Returns the dictionary implementing the current module’s global namespace.

Version: Python 3.14; repository image, production runtime not attested

Syntax

globals()

Returns the dictionary implementing the current module’s global namespace.

A function-local binding can shadow a global with the same name while a dictionary lookup still reads the module binding.

Returns the live dictionary of the module in which the current code was defined. A function keeps its defining module’s globals even when called from elsewhere. The local assignment size=2 shadows module size=9, so the function prints 2 and then 9 from the dictionary.

module_value = 9
def f():
    module_value = 2
    return module_value, globals()["module_value"]
assert f() == (2, 9) and f.__globals__ is globals()

Examples

size = 9
def sample():
    size = 2
    print(size, globals()["size"])

sample()

Result

2 9

Returns the live dictionary of the module in which the current code was defined. A function keeps its defining module’s globals even when called from elsewhere. The local assignment size=2 shadows module size=9, so the function prints 2 and then 9 from the dictionary.

Sources