Python | locals() function

22 Sep, 2021
Python locals() function returns the dictionary of the current local symbol table.
Symbol table: It is a data structure created by a compiler for which is used to store all information needed to execute a program.
Local symbol Table: This symbol table stores all information needed for the 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.
Python3
# Python program to understand about locals# here no local variable is present def demo1():    print("Here no local variable  is present : ", locals()) # here local variables are presentdef demo2():    name = "Ankit"    print("Here local variables are present : ", locals()) # driver codedemo1()demo2()
Output;
Here no local variable  is present :  {}
Here local variables are present :  {'name': 'Ankit'}
Unlike globals() this function can not modify the data of the local symbol table. The below program explains it clearly. 
Python3
# Python program to understand about locals# here no local variable is present def demo1():    print("Here no local variable  is present : ", locals()) # here local variables are presentdef 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 codedemo1()demo2()
Output:
Here no local variable  is present :  {}
Here local variables are present :  {'name': 'Ankit'}
Before updating name is  :  Ankit
after updating name is :  Ankit
The local symbol table is the same as the global symbol table in the case of the global environment. 
Python3
# Python program to understand about locals # data using localsprint("This is using locals() : ", locals()) # data using globalsprint("This is using globals() : ", globals())
Output:
This is using locals() :  {‘__name__’: ‘__main__’, ‘__doc__’: ‘Automatically created module for IPython interactive environment’, ‘__package__’: None, ‘__loader__’: None, ‘__spec__’: None, ‘__builtin__’: <module ‘builtins’ (built-in)>, ‘__builtins__’: <module ‘builtins’ (built-in)>, ‘_ih’: [”, ‘import multiprocessing\nfrom bs4 import BeautifulSoup\nfrom queue import Queue, Empty\nfrom concurrent.futures import ThreadPoolExecutor\nfrom urllib.parse import urljoin, urlparse\nimport requests\n\n\nclass MultiThreadedCrawler:\n\n    def __init__(self, seed_url):\n        self.seed_url = seed_url\n        self.root_url = \'{}://{}\’.format(urlparse(self.seed_url).scheme,\n                                         urlparse(self.seed_url).netloc)\n        self.pool = ThreadPoolExecutor(max_workers=5)\n        self.scraped_pages = set([])\n        self.crawl_queue = Queue()\n        self.crawl_queu
The python globals() function in Python returns the dictionary of the current global symbol table.
Syntax: globals()

Parameters: No parameters required.
Python3
# Python3 program to demonstrate global() function # global variablea = 5 def func():    c = 10    d = c + a         # Calling globals()    globals()['a'] = d    print (d)     # Driver Code   func()
Output:
15
Python locals() function in Python returns the dictionary of the current local symbol table.
Python3
locals()
Output:
{‘__name__’: ‘__main__’,
 ‘__doc__’: ‘Automatically created module for IPython interactive environment’,
 ‘__package__’: None,
 ‘__loader__’: None,
 ‘__spec__’: None,
 ‘__builtin__’: <module ‘builtins’ (built-in)>,
 ‘__builtins__’: <module ‘builtins’ (built-in)>,
 ‘_ih’: [”,
  ‘# Python program to demonstrate the use of\n# len() method  \n\n# Length of below string is 5\nstring = “geeks” \nprint(len(string))\n\n# Length of below string is 15\nstring = “geeks for geeks” \nprint(len(string))’,
  ‘# Python program to demonstrate the use of\n# len() method  \n\n# L
Akanksha_Rai
kumar_satyam
Python-Functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function