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
def describe(name, age):
note = "example"
return locals()
print(describe("Ada", 36))
Output
{'name': 'Ada', 'age': 36, 'note': 'example'}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
| Version | Change |
|---|---|
| Python 3.0 | Added |
| Python 3.13 | Defined semantics in functions: each call returns a new snapshot (PEP 667). |
Related
globals()· Return the dictionary of the current module's global names: variables, functions, classes and imports.vars()· Return the __dict__ of a module, class or object: its writable attributes as a dictionary. With no argument, like locals().dir()· List the names an object has, such as attributes and methods. With no argument, list the names in the current scope.- Python Scope · tutorial