Python | locals() function
locals() function in Python returns the dictionary of current local symbol table.
Symbol table: It is a data structure created by compiler for which is used to store all information needed to execute a program.
Local symbol Table: This symbol table stores all information which needed to local scope of the program and this information is accessed using python built in function locals().
Syntax : locals()
Parameters: This function does not takes any input parameter.
Return Type : This returns the information stored in local symbol table.
Example #1:
# Python program to understand about locals # here no local varible is present def demo1(): print("Here no local variable is present : ", locals()) # here local variables are present def demo2(): name = "Ankit" print("Here local variables are present : ", locals()) # driver code demo1() demo2() |
Here no local variable is present : {}
Here local variables are present : {'name': 'Ankit'}
Example #2: Updating using locals().
Unlike from globals() this function can not modify the data of local symbol table. Below program exaplains it clearly.
# Python program to understand about locals # here no local varible is present def demo1(): print("Here no local variable is present : ", locals()) # here local variables are present def demo2(): name = "Ankit" print("Here local variables are present : ", locals()) print("Before updating name is : ", name) # trying to change name value locals()['name'] = "Sri Ram" print("after updating name is : ", name) # driver code demo1() demo2() |
Here no local variable is present : {}
Here local variables are present : {'name': 'Ankit'}
Before updating name is : Ankit
after updating name is : Ankit
Example #3: locals() for global environment.
Local symbol table is same as global symbol table in the case of global environment.
# Python program to understand about locals # data using locals print("This is using locals() : ", locals()) # data using globals print("This is using globals() : ", globals()) |
This is using locals() : {‘__file__’: ‘/home/34dde64e1e47944021cdf478b97f13a0.py’, ‘__doc__’: None, ‘__name__’: ‘__main__’, ‘__cached__’: None, ‘__spec__’: None, ‘__builtins__’:
, ‘__package__’: None, ‘__loader__’: <_frozen_importlib_external.SourceFileLoader object at 0x7f885e463470>}
This is using globals() : {‘__file__’: ‘/home/34dde64e1e47944021cdf478b97f13a0.py’, ‘__doc__’: None, ‘__name__’: ‘__main__’, ‘__cached__’: None, ‘__spec__’: None, ‘__builtins__’:, ‘__package__’: None, ‘__loader__’: <_frozen_importlib_external.SourceFileLoader object at 0x7f885e463470>}
Recommended Posts:
- id() function in Python
- Python map() function
- Help function in Python
- ord() function in Python
- sum() function in Python
- Python | int() function
- Python | hex() function
- Python | oct() function
- Python | now() function
- Python | dir() function
- Python | cmp() function
- Python | How to get function name ?
- Sum 2D array in Python using map() function
- Python | math.cos() function
- Python | property() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



