In this tutorial, we will learn about the python locals() method and its uses with examples.
What is the Python locals() method?
The python locals() method returns the dictionary of the current local symbol table.
What is the symbol table in python?
A symbol table is a data structure containing all the necessary information regarding the program, including variable names, methods, methods, classes, etc.
The syntax of locals() method is:
The syntax of globals() method is:
The locals() method does not take any parameter.
Let us check some examples of the locals() method.
Using the locals() method in the python interpreter will show all the local variables and other default symbols declared by python.
Example 1: Using locals() with a python interpreter.
When you run the above program, it will display the following output on the python interpreter.
python locals globals symbol tables for the global environment are the same.
Example 2: How to use the locals() method in python?
The output will be as follow.
Example 3: Updating locals() dictionary values.
Rules of locals()
- Python locals() method updates and returns a dictionary associated with the current local symbol table.