Python Tutorial Python Advanced Python References Python Libraries

Python globals() Function



The Python globals() function returns the global table dictionary. The table contains all the necessary information about the current program.

Syntax

globals()

Parameters

No parameter is required.

Example:

In the example below, the globals() is used to return the current global table dictionary.

x = globals()
print(x)

The output of the above code will be:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fc71d61fa60>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'Main.py', '__cached__': None, 'x': {...}}

Example:

The globals() function can also be used to retrieve only required information about the current program. Like in the example below, it is used to retrieve the name of the current program only.

x = globals()
print(x["__name__"])

The output of the above code will be:

__main__

❮ Python Built-in Functions