Python 3 reference · Built-in Functions

Python locals() Function

Return a dictionary of the local variables in the current scope. Inside a function it is a snapshot since Python 3.13.

Python 3 Since 3.0

Syntax

locals()

Return value

A mapping of local names to values.

Examples

Local variables of a functionTry it yourself
def describe(name, age):
    note = "example"
    return locals()

print(describe("Ada", 36))

Output

{'name': 'Ada', 'age': 36, 'note': 'example'}
Fill a template from localsTry it yourself
def greet(name, greeting="Hello"):
    return "{greeting}, {name}!".format(**locals())

print(greet("Ada"))

Output

Hello, Ada!

Good to know

Since Python 3.13 (PEP 667), locals() inside a function returns an independent snapshot: changing the dictionary does not change the variables.

Version history

VersionChange
Python 3.0Added
Python 3.13Defined semantics in functions: each call returns a new snapshot (PEP 667).