Python locals()
Preview: Anonymous contributor 1 total contribution
Anonymous contributor
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 clipboardCopy to clipboard
The code above generates the following output:
No local variables. {}One local variable. {'ex': 'A'}Copy to clipboardCopy 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 usVisit usCodeHide codeOutputHide outputCopy to your clipboard
All contributors
- Preview: Anonymous contributor 1 total contribution
- Anonymous contributor
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
