locals() function in Python
Last Updated :
15 May, 2025
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:
Python
def fun(a, b):
res = a + b
print(locals())
fun(3, 7)
Output{'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.
Symbol table in Python
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. |
Syntax of locals()
locals()
Parameters: This function does not take any input parameter.
Return Type: This returns the information stored in local symbol table.
Behavior of locals() in different scopes
Example 1: Using locals() with no local variables
Python
def fun():
print(locals())
fun()
Explanation: Since there are no local variables inside fun(), locals() returns an empty dictionary.
Example 2: Using locals() with local variables
Python
def fun():
user = "Alice"
age = 25
print(locals())
fun()
Output{'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.
Can locals() modify local variables?
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()
Python
def fun():
name = "Ankit"
# attempting to modify name
locals()['name'] = "Sri Ram"
print(name)
fun()
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.
Using locals() in global Scope
In the global scope, locals() behaves just like globals() since the local and global symbol tables are the same. Example:
Python
x = 10
print(locals())
print(globals())
Output{'__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.
Practical examples
Example 1: Here, we use locals() to access a local variable using a dictionary-style lookup.
Python
def fun():
user = "Vishakshi"
print("Hello", locals()['user'])
fun()
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.
Python
def fun(name, age):
t = "My name is {name} and I am {age} years old."
print(t.format(**locals()))
fun("Vishakshi", 22)
OutputMy 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.
global() vs locals()-Comparison Table
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 |
---|
Related articles
Similar Reads
locals() function-Python 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:Pythondef fun(a, b): res = a + b print(locals()) fun(3, 7)Output{'a': 3, 'b': 7, 'res': 10} Exp
4 min read
Python - globals() function In Python, the globals() function is used to return the global symbol table - a dictionary representing all the global variables in the current module or script. It provides access to the global variables that are defined in the current scope. This function is particularly useful when you want to in
2 min read
Partial Functions in Python Partial functions allow us to fix a certain number of arguments of a function and generate a new function. In this article, we will try to understand the concept of Partial Functions with different examples in Python.What are Partial functions and the use of partial functions in Python?Partial funct
5 min read
Python Functions Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an
9 min read
Python print() function The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Python Inner Functions In Python, a function inside another function is called an inner function or nested function. Inner functions help in organizing code, improving readability and maintaining encapsulation. They can access variables from the outer function, making them useful for implementing closures and function dec
5 min read
locals() function in Python
min read