VOOZH about

URL: https://www.codecademy.com/resources/docs/python/built-in-functions/locals

⇱ Python | Built-in Functions | locals() | Codecademy


Skip to Content

Python locals()

Preview: Anonymous contributor 1 total contribution
Anonymous contributor

1 total contribution

Published Oct 4, 2024

The locals() built-in function in Python returns a dictionary containing the current local symbol table, which maps variable names to their values in the current scope.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 CoursesIncludes 6 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      75 hours75 hours

Syntax

locals()

Example

The following example demonstrates the usage of the locals() function:

defexample1():
print("No local variables.",locals())
defexample2():
ex="A"
print("One local variable.",locals())
example1()
example2()
Copy to clipboard
Copy to clipboard

The code above generates the following output:

No local variables. {}
One local variable. {'ex': 'A'}
Copy to clipboard
Copy to clipboard

Note: locals() cannot change the local symbol table. It only allows seeing it.

Codebyte Example

Run the following code to understand how the locals() function works:

Visit us
Visit us
Hide code
Code
Output
Hide output
Copy to your clipboard

All contributors

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 CoursesIncludes 6 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      75 hours75 hours