In Python, locals() returns a dictionary of the local names in the current scope. Inside a function, that means its parameters and the variables assigned so far. At the top level of a module, locals() and globals() are the same dictionary.
Inside a function, treat the result as a read-only snapshot: writing to it does not change the real variables. Python 3.13 made this behaviour officially defined; earlier versions acted the same way for plain functions.
locals() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
It takes no arguments.
python
current = locals()
Call
What it does
locals()
Dict of the current local names.
"x" in locals()
Is there a local named x?
"{a}".format(**locals())
Format with local variables.
locals() is globals()
True at module level.
Comprehensions and generator expressions have their own scope in Python 3, so a locals() call inside one sees only that scope.
Library declarations
python
locals()
Parameters
locals() takes no arguments.
Return Value
Return Value
Description
dict
The current local names and their values.
Python Examples
Example 1 — See a Function's Local Variables
Return locals() from a function and inspect it.
python
def describe(name, age):
city = "Oslo"
return locals()
info = describe("Ana", 31)
print(info)
print(sorted(info))