Python Reference Library →

Python locals() Function – Get Local Symbol Table as a Dictionary

Let’s look at locals() function – get local symbol table as a dictionary

  1. Notice

    locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
def show(number):
    namespace = locals()
    print(namespace["number"])
show(3)
Live state

Top-level scope

show→Object 1
Object 1 · functionfunction show
This step

locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.

Picture locals() function – get local symbol table as a dictionary

locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.

Image brief

Alt text
A diagram for Python locals() Function – Get Local Symbol Table as a Dictionary. Instructions: def show(number): namespace = locals() print(namespace["number"]) show(3). Displayed result: 3. The final example bindings are show --> object 1 (function). object 1: function show
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python locals() Function – Get Local Symbol Table as a Dictionary". Teaching focus: locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS def show(number): namespace = locals() print(namespace["number"]) show(3) | v DISPLAYED RESULT 3 FINAL BINDINGS show --> object 1 (function) object 1: function show Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python locals() Function – Get Local Symbol Table as a Dictionary. Instructions: def show(number): namespace = locals() print(namespace["number"]) show(3). Displayed result: 3. The final example bindings are show --> object 1 (function). object 1: function show

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
def show(number):
    namespace = locals()
    print(namespace["number"])
show(3)
Live state

Top-level scope

show→Object 1
Object 1 · functionfunction show
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3

What do you expect this program to print?

What does this example display?

def show(number):
    namespace = locals()
    print(namespace["number"])
show(3)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3

Follow the printed values in order. The output is: 3

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.

locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.

More examples and reference

Python locals() Function

The locals() function in Python returns a dictionary representing the current local symbol table. This means it gives you a snapshot of all local variables and their values at a particular point in your code.

Syntax

locals()

Parameters:

  • None – This function takes no arguments.

Returns:

  • A dict object containing all local variables in the current scope.

Example 1: Using locals() in a Function

def show_locals():
    a = 10
    b = "Hello"
    print(locals())

show_locals()
{'a': 10, 'b': 'Hello'}

Example 2: Using locals() in the Global Scope

x = 5
y = 7
print(locals())

Output (in interactive mode):

{'x': 5, 'y': 7, ...}

In a script, locals() at the global level behaves like globals().

Use Case: Debugging and Dynamic Variable Handling

  • Helpful during debugging to inspect current variable states.
  • Used in advanced applications like dynamic form builders or interpreters.
  • Can be passed to eval() or exec() to evaluate expressions in the current local context.

Modifying the Returned Dictionary

While you can modify the dictionary returned by locals(), these changes may not affect actual local variables (especially inside functions).

Example:

def test():
    x = 1
    loc = locals()
    loc['x'] = 99
    print("x =", x)

test()
x = 1

Why? Because changes to the dictionary don’t always reflect back in the local scope.

Common Mistakes

  • Assuming modifying locals() will update variables – it might not.
  • Using locals() for simple scripts unnecessarily can confuse beginners.

Interview Tip

Interviewers might ask about the difference between locals() and globals(). Remember: locals() gives variables in the current function or scope, while globals() gives variables at the module level.

Summary

  • locals() returns a dictionary of current local variables.
  • Used for debugging, dynamic evaluation, and introspection.
  • Modifying the dictionary might not change actual variables inside a function.

Practice Problem

Write a function where you declare 3 variables, print them, and also print locals(). Observe the difference.

def demo():
    a = 1
    b = 2
    c = a + b
    print("Sum is:", c)
    print("Local variables:", locals())

demo()

What to remember

Connect what you noticed in the example with the idea below.

Remember

locals() provides the local namespace at the point of the call. Do not use it as a portable way to rebind function-local names.

Watch out for this idea

locals() contains only names from imported modules.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →