![]() |
In Python, the globals() function is a built-in function that returns a dictionary representing the current global symbol table. In this article, we will see the Python globals() function. globals() Function in PythonPython globals() function is a built-in function in Python that returns the dictionary of the current global symbol table. Symbol table: A symbol table is a data structure that contains all necessary information about the program. These include variable names, methods, classes, etc. The global symbol table stores all information related to the program’s global scope and is accessed in Python using the globals() method. The functions, and variables that are not associated with any class or function are stored in global scope. Python globals() Syntax
Example 1: How Globals Python Method WorksIn this example, we are using the globals() function to display the global symbol table before any variables are defined as well as displaying the global symbol table after variables are defined. Python3
Output
Example 2: Python globals() Function DemonstrationIn this example, we are using globals() function to demonstrate about globals() function in Python. Python3
Output
15 Example 3: Modify Global Variable Using Globals in PythonIn this example, we are using globals() to modify global variables in Python. Python3
Output
Before modification: gautam After modification: gautam karakoti Note: We can also change the value of global variables by using globals() function. The changed value also updated in the symbol table. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 8 |