Tuesday, September 24, 2024
Google search engine
HomeLanguagesPython | locals() function

Python | locals() function

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.

Python locals() method example

Example 1: Python locals() works insides a local scope

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

Example 2: Updating using locals()

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

Example 3: 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 VS global functions

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 variable
a = 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 = “Lazyroar” \nprint(len(string))\n\n# Length of below string is 15\nstring = “Lazyroar for Lazyroar” \nprint(len(string))’,

  ‘# Python program to demonstrate the use of\n# len() method  \n\n# L

RELATED ARTICLES

Most Popular

Recent Comments