Python locals()
Python locals() builtin function is used to get the current local symbol table as a dictionary.
In this tutorial, we will learn about the syntax of Python locals() function, and learn how to use this function with the help of examples.
Syntax
The syntax of locals() function is
</>
Copy
locals()
Returns
The function returns a dictionary.
Example
In this example, we initialize two variables: a
and b
, and get the local symbol table using locals() function. Along with the boilerplate symbols, there would be variables a
and b
in the dictionary returned by locals().
Python Program
</>
Copy
a = 2
b = 3
result = locals()
print(result)
Output
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x101073d60>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/tutorialkart/Desktop/Projects/PythonTutorial/Example.py', '__cached__': None, 'a': 2, 'b': 3, 'result': {...}}
Conclusion
In this Python Tutorial, we have learnt the syntax of Python locals() builtin function, and also learned how to use this function, with the help of Python example programs.