Open In App

locals() function in Python

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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()

Output
{}

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()

Output
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.

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()

Output
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.

Python
def fun(name, age):
    t = "My name is {name} and I am {age} years old."
    print(t.format(**locals()))

fun("Vishakshi", 22)

Output
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.

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


Article Tags :
Practice Tags :

Similar Reads