locals() Function in Python Examples

Python locals() works insides a local scope

In this example, we are using the locals() function to show how it works inside a local scope. In demo1, no local variables are defined, so calling locals() returns an empty dictionary. In demo2, a local variable named name is defined, and calling locals() returns a dictionary containing the “name” variable with the value “Ankit.”

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 present
def demo2():
    name = "Ankit"
    print("Here local variables are present : ", locals())
 
# driver code
demo1()
demo2()


Output

Here no local variable  is present :  {}
Here local variables are present :  {'name': 'Ankit'}





Updating using locals()

Unlike globals() this function can not modify the data of the local symbol table. We can only access the local symbol table in Python by using it. 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 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()


Output

Here no local variable  is present :  {}
Here local variables are present :  {'name': 'Ankit'}
Before updating name is  :  Ankit
after updating name is :  Ankit





locals() for global environment

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 locals
print("This is using locals() : ", locals())
 
# data using globals
print("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

Python | locals() function

Python locals() function returns the dictionary of the current local symbol table. We use it to access the local symbol table in Python.

  • Symbol table: It is a data structure created by a compiler that 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 the Python built-in function locals().

Similar Reads

Python locals() Function Syntax

We can access the local symbol table in Python by using locals() function....

locals() Function in Python Examples

Python locals() works insides a local scope...

Python locals VS global functions

...

Frequently Asked Questions

...