Menu Close

Python globals() Function

python global() function

In this tutorial guide, we are going to learn all about the Python globals function to find the globals symbol table as a dictionary.
globals() function is the best built-in function to find the globals symbols symbol. In the previous tutorial, we have seen Python getattr function to find the value specified attribute of the specified object.

Python globals() Function

Python globals function is a part of Python built-in functions that is used to find the global symbol table as a dictionary.

Syntax

The syntax of global() function in Python is:-

globals()

Parameter:

globals function in python does not support any parameter.

Return Value

The return value of the globals function in Python is:-

Python globals() example

Here we will use the Python globals function along with an example.

Example 1:


symbols = globals()
print(symbols)

Output

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000019202190AF0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:\\Users\\Vishvajit\\Desktop\\PF Article\\Python Basic part1\\test.py', '__cached__': None, 'symbols': {...}}

Example 2:


x = globals()
print(x["__file__"])
print(x['__name__'])
print(x['__builtins__'])

Output

C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py
__main__
<module 'builtins' (built-in)>

Conclusion

So, In this tutorial, you have learned all about Python globals function to find the symbols as a dictionary. If this article helps you, please keep visiting for further python built-in functions tutorials.

Python built-in functions


For more information:- Click Here

Python eval() Function
Python hex() Function

Related Posts