In Python, locals() is a built-in function that returns a dictionary containing all local variables in the current scope. Inside a function, locals() returns that function's local variables. At the module level, locals() returns global variables.

When used inside a function, modifying the dictionary returned by locals() does not affect the values of local variables.

However, at the module level it's different. In the global scope, locals() is essentially the same as globals(). Reference:

a = 123

def foo():
    x = 3.14
    y = "hello"

    print(locals())
    # Cannot modify in local scope
    locals()['x'] = 5
    print(x)

foo()
# Can modify in global scope
locals()['a'] = 456
print(a)

Program Output:

{'x': 3.14, 'y': 'hello'}
3.14
456