![]() |
VOOZH | about |
locals() function in Python returns a dictionary representing the current local symbol table, allowing you to inspect all local variables within the current scope e.g., inside a function or block. Example:
{'a': 3, 'b': 7, 'res': 10}
Explanation: fun() defines three local variables a, b and res. The locals() function returns a dictionary of these variables and their values.
A symbol table is a data structure maintained by the Python interpreter that stores information about the program’s variables, functions, classes, etc.Python maintains different types of symbol tables:
Type | Description |
|---|---|
Local Symbol Table | Stores information about variables defined within a function/block |
Global Symbol Table | Contains info about global variables and functions defined at top level |
Built-in Symbol Table | Contains built-in functions and exceptions like len(), Exception, etc. |
locals()
Parameters: This function does not take any input parameter.
Return Type: This returns the information stored in local symbol table.
Example 1: Using locals() with no local variables
{}
Explanation: Since there are no local variables inside fun(), locals() returns an empty dictionary.
Example 2: Using locals() with local variables
{'user': 'Alice', 'age': 25}
Explanation: fun() define two local variables user and age. When we call locals(), it returns a dictionary showing all the local variables and their values within that function's scope.
Unlike globals(), which can modify global variables, locals() does not allow modifying local variables inside a function.
Example: Attempting to modify local variables using locals()
Ankit
Explanation: Even though locals()['name'] is modified, the change does not reflect in the actual variable because locals() returns a copy of the local symbol table, not a live reference.
In the global scope, locals() behaves just like globals() since the local and global symbol tables are the same. Example:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f5ee5639e00>, '__spec__': None, '__annotations__': {}, '__builtin...Explanation: In global context, locals() and globals() refer to the same dictionary.
Example 1: Here, we use locals() to access a local variable using a dictionary-style lookup.
Hello Vishakshi
Explanation: fun() creates a local variable user. Instead of accessing it directly, locals()['user'] retrieves its value dynamically.
Example 2: This example shows how locals() can be used with string formatting to inject local variable values into a template string.
My name is Vishakshi and I am 22 years old.
Explanation: locals() provides the local variables name and age as keyword arguments to format(). This makes the code more flexible and avoids writing .format(name=name, age=age) manually.
Knowing the difference between the two helps you debug better, write smarter code and even modify behavior dynamically when needed. Let’s understand them through the following table:
Feature | globals() | locals() |
|---|---|---|
Returns | Global symbol table | Local symbol table |
Scope Level | Global/module-level variables and functions | Variables inside the current function/block |
Modifiable? | Yes, modifies global variables | No, does not modify local variables |
Access global variables? | Yes | No (inside a function) |
Behavior in global scope | Same as locals() | Same as globals() |
Used for? | Modifying and accessing global variables | Inspecting local variables |