- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Globals() Function in Python
In Python, every program has a symbol table that contains the information about its names (variables, functions, and classes) defined in the program. The global() function allows us to access and manipulate the global symbol table, providing valuable information about the current global variables in a program. In this article, we will understand how these globals() function works and what we can do with this function in Python.
Overview of globals() function
In Python, each program has a symbol table that contains information about the names (variables, functions, classes, etc.) defined in the program. The symbol table is represented by a dictionary, where the names are the keys, and the associated values represent their current values or references.
The globals() function returns a dictionary representing the current global symbol table. It provides access to all the global variables and their corresponding values in the current scope. By examining this dictionary, we can retrieve, modify, or delete global variables programmatically.
Syntax
globals()
Here, the globals() function doesn't take any arguments and it simply returns the dictionary representing the global symbol table.
Retrieving Global variables
Retrieving global variables in Python can be accomplished using the globals() function, which returns a dictionary representing the current global symbol table. By accessing this dictionary and iterating over its items, you can retrieve the names and values of all global variables in the current scope.
Example
In the below example, we have defined three global variables: x, y, and z. We also define a function print_globals() that calls the globals() function to retrieve the global symbol table. By iterating over the items of the resulting dictionary, we print the names and values of all the global variables. Additionally, we can see that the function print_globals() itself is also present in the global symbol table.
x = 10 y = 5 z = "Hello, World!" def print_globals(): global_vars = globals() for name, value in global_vars.items(): print(f"{name}: {value}") print_globals()
Output
__name__: __main__ __doc__: None __package__: None __loader__: <_frozen_importlib_external.SourceFileLoader object at 0x7f9b30a74a30> __spec__: None __annotations__: {} __builtins__: <module 'builtins' (built-in)> __file__: /home/cg/root/22219/main.py __cached__: None x: 10 y: 5 z: Hello, World! print_globals: <function print_globals at 0x7f9b30c13d90>
Modifying Global Variables
The globals() function not only allows us to retrieve global variables but also allow us to modify their values.
Example
In the below example, we define a global variable x with an initial value of 10. The modify_global() function retrieves the global symbol table using globals() and modifies the value of x to 20 by directly updating the dictionary entry. When we print the value of x before and after calling the modify_global() function, we can see that the modification takes effect.
x = 10 def modify_global(): global_vars = globals() global_vars['x'] = 20 print("Before modification:", x) modify_global() print("After modification:", x)
Output
Before modification: 10 After modification: 20
Adding New Global Variables
We can also add new global variables dynamically using the globals() function. Let’s see an example demonstrating the same.
Example
In the below example, we define a function add_global() that adds a new global variable named new_var to the global symbol table using globals(). After calling the function, we print the value of new_var, which confirms that the variable has been successfully added and is accessible globally.
def add_global(): global_vars = globals() global_vars['new_var'] = "I'm a new global variable!" add_global() print(new_var)
Output
I'm a new global variable!
Conclusion
In this article, we discussed what is globals() function is and why it is used for. The globals() function is used to access and manipulate global variables within a program. By using this function, we can retrieve, modify, or add global variables dynamically. By using the power of the globals() function, you can gain better control over your program's global variables, thereby enhancing your Python programming skills.