all Python 3Built-in functionsterm
locals
namespaces
Returns a dictionary of the current local namespace. At module level it is the very object globals() returns; inside a function it is a snapshot, not a reference.
Syntax
locals()
Inside a function the result is a snapshot: writing into that dict will not create or change a local variable, because there is no link back to the frame (Python 3.13 guarantees this via PEP 667; before that it was implementation-defined). At module level and in a class body locals() hands you the real namespace mapping, where writes do take effect — which is the usual source of confusion. Only names already assigned at the moment of the call show up inside a function.
Examples
def f():
x = 10; y = 20
return locals()
print(f()) # → {'x':10,'y':20}
def g(a, b):
c = a+b
print('c' in locals()) # → True
return locals()
print(g(1,2)) # → {'a':1,'b':2,'c':3}
print(type(locals())) # → <class 'dict'>
See also
Open in the full glossarydocs.python.orgReport an inaccuracy