Python globals() is a built-in function that returns the dictionary containing the current global namespace or current global symbol table.

python globals() function

The dictionary returned by Python globals() function contains keys which are globally defined names and values are the corresponding values of those globally defined names.

These global namespaces are used for functions or variables that are not associated with a particular class or function.

Python globals() Syntax

globals()

As you can see in above syntax, the globals() function doesn’t take any arguments.

The dictionary returned by the globals() function is the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

Python globals() Example

>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None,
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
 '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}

As you can see in above example, the globals() functions return the dictionary representing all the global variables associated with the current program.