Python locals() built-in function

From the Python 3 documentation

Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks. Note that at the module level, locals() and globals() are the same dictionary.

Introduction

The locals() function returns a dictionary representing the current local symbol table. This includes all local variables, arguments, and other objects in the current scope.

It’s a useful tool for inspecting the local namespace.

Example

def my_function(arg1, arg2):
    local_var = "I am local"
    print(locals())

my_function("hello", "world")
# Output will be something like:
# {'arg1': 'hello', 'arg2': 'world', 'local_var': 'I am local'}

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.