Python globals() Function
The Python globals() function is used to return 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 below example, the globals() is used to return the current global table dictionary.
x = globals() print(x)
The output of the above code will be:
{ 'sys': <module 'sys' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, 'x': {...}, '__cached__': None, '__doc__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object>, '__file__': './main.py', '__spec__': None, '__package__': None, '__name__': '__main__' }
Example:
The globals() function can also be used to retrieve only required information about the current program. Like in the below example, 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