The built-in locals()
function returns a dictionary representing the current local symbol table, which includes variable names as keys and their currently bound references as values. This function is useful for inspecting the state of the local scope within a function or other local contexts:
locals()
Signature Arguments
locals()
function doesn’t take any arguments.locals()
Examples
With a function to display local variables:
When called in the global scope:
locals()
Common Use Cases
The most common use cases for the locals()
function include:
locals()
Real-World Example
You might want to log the state of local variables for debugging purposes when a function fails:
In this example, the locals()
function provides a snapshot of the local variables when an error occurs, which can be invaluable for debugging.
Tutorial
Namespaces in PythonIn this tutorial, you'll learn about Python namespaces, the structures that store and organize the symbolic names during the execution of a Python program. You'll learn when namespaces are created, how they're implemented, and how they support variable scope.
For additional information on related topics, take a look at the following resources:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4