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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to understand about locals
  
# data using locals
print("This is using locals() : ", locals())
  
# data using globals
print("This is using globals() : ", globals())

chevron_right


Output:

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>}



My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

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.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.