Python locals() function
Python locals() function
The locals() function in Python updates and returns a dictionary representing the current local symbol table.
Syntax
locals()
Parameter
NA
Return
This function returns the local symbol table as a dictionary or returns free variables when it is called in function blocks, but not in class blocks.
Example 1
# Python program explaining # the locals() function val = locals() print(val)
Output
{'__package__': None, '__builtins__': <module 'builtins' (built-in)>, '__loader__':
<_frozen_importlib.SourceFileLoader object at 0x7fdb0675ae48>, '__name__': '__main__', '__file__':
'main.py', '__spec__': None, 'val': {...}, '__cached__': None, '__doc__': None}
Example 2
# Python program explaining
# the locals() function
def Example1():
print("Local variable returns: ", locals())
# passing local variables
def Example2 ():
name = "Reema"
print("Local variables returns: ", locals())
# invoking the class
Example1()
Example2()
Output
Local variable returns: {}
Local variables returns: {'name': 'Reema'} 