How to Use globals() Function in PythonPython Basics


The globals() function returns the dictionary of global symbol table. It stores all information related to the global scope of the current program.

Example


str = 'ABC'
print(globals())
print('The value of str is:', globals()['str'])

# We can modify global variable
globals()['str'] = 'XYZ'
print('The new value of str is:', str)

After running the program, you can see the output as below. The value of string has been changed from ‘ABC’ to ‘XYZ’.


{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f2bb5f01358>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'signal': <module 'signal' from '/usr/local/lib/python3.6/signal.py'>, '__file__': 'main.py', '__cached__': None, 'str': 'ABC'}
The value of str is: ABC
The new value of str is: XYZ

Syntax


globals()

Parameters

The method takes no parameters.

Return Value

It returns the global symbol table as a dictionary.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us