Hey, folks! In this article, we talk about the Python globals() function in detail.
So, let us get started.
What is Python globals() function?
Python globals() function
enables us to access the list of all the global variables and their values in the particular code throughout the program.
The globals() function represents a Python dictionary that contains the current global symbol table.
A question might arise in your mind..
What is a Symbol Table?
A Symbol table is a table structure that represents information about different symbols. Symbols can be any attribute of the programing code such as variables, keywords, functions, etc.
Moreover, the symbol table specifies the names of these mentioned symbols along with the type of their objects, their scope throughout the program, etc.
Symbol table can be classified into the below types:
- global table: Represents information in the form of dictionary about the global variables.
- local table: Represents information in the form of dictionary about the local variables.
Python locals() function
represents and returns the current local symbol table. To know more about local variables and locals() function, do visit Python locals() function.
Having understood the working of globals() function, let us now understand the structure of the same as mentioned in the below section.
Syntax of globals() function
The globals() function
does not accept any parameter. It returns the dictionary that presents the values of the current symbol table.
Syntax:
globals()
Now, let us understand the implementation of globals() function through the below examples.
Implementing Python globals() function through examples
In the below example, we have called be globals() function in order to interpret the output as shown:
Example:
print(globals())
The globals() function returns a dictionary of values from the global symbol table, which provides details about the module, file name, etc.
Output:
{'__cached__': None, '__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '__spec__': None, '__file__': 'main.py', '__doc__': None, '__loader__': <_frozen_importlib.SourceFileLoader object at 0x7f31606f4e80>, '__package__': None}
Display Global and Local Scope Variables
Next, we will define a global and local variable within a function and have called the globals() function to know the outcome.
Example:
varG = 20 # global variable
def var():
varL = 100 # local variable
print(globals())
The Python globals() function returns a dictionary that gives information about the global variable (varG) and its value along with the function, file name, etc.
But, if you notice, the globals() function has not represented information about the local variable (varL). That work would be served by locals() function.
Output:
{'__file__': 'main.py', 'varG': 20, '__loader__': <_frozen_importlib.SourceFileLoader object at 0x7f7ff13e7e48>, '__cached__': None, '__doc__': None, '__package__': None, '__name__': '__main__', '__spec__': None, '__builtins__': <module 'builtins' (built-in)>, 'var': <function var at 0x7f7ff1436bf8>}
Modifying and Manipulating Variables
We can even modify the value of the global variable as shown below:
Example:
var = 100
globals()['var'] = 12
print('var:', var)
print(globals())
Here, we have assigned var = 100. Further, we have changed the value of the variable ‘var’ to 12 using globals() function as shown above.
If we analyze the output, the globals function returns the dict of the global symbol table with the updated value of our variable i.e. var = 12
Output:
var: 12
{'__file__': 'main.py', '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, '__spec__': None, '__name__': '__main__', '__package__': None, '__loader__': <_frozen_importlib.SourceFileLoader object at 0x7f3f83a1ae10>, '__cached__': None, 'var': 12}
Conclusion
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. Till then, Happy Learning!!
References
- Python global variables — JournalDev