Change language

Python | locals () function

|

Symbol table: is a compiler-generated data structure used to store all the information needed to run a program. 
Local symbol table: This symbol table stores all the information required for the local scope of a program and is accessed using the Python built-in locals () function .

Syntax: locals ()

Parameters: This function does not take any input parameter.

Return Type: This returns the information stored in local symbol table.

Example # 1:

# Python program for understanding locals

 
# no local variable here

def demo1 () :

print (  "Here no local variable is present:" , locals ())

 
# local variables are present here

def demo2 ():

name = "Ankit"

print ( "Here local variables are present:" , locals ())

 
# driver code
demo1 ()
demo2 ()

Exit :

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

Example # 2: Updating using locals () .

Unlike globals (), this function cannot modify local symbol table data. The program below explains it clearly.

# Python program for understanding locals

  
# no local variable here

def demo1 ():

print ( " Here no local variable is present: " , locals ())

 
# local variables are present here

def demo2 ():

name = "Ankit"

print ( "Here local variables are present:" , locals ())

print ( "Before updating name is :" , name)

 

# trying change name value

locals () [ ’name’ ] = "Sri Ram"

 

print ( "after updating name is:" , name)

  
# driver code
demo1 ()
demo2 ()

Exit:

 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 the global environment .

The local symbol table is the same as the global symbol table for the global environment.

# Python program for understanding locals

 
# data using locals

print ( "This is using locals ():" , locals ())

 
# data using globals

print ( "This is using globals ():" , globals ())

Exit:

This is using locals (): {’__file__’: ’/ home / 34dde64e1e47944021cdf478b97f13a0.py ’,’ __doc__ ’: None,’ __name__ ’:’ __main__ ’,’ __cached__ ’: None,’ __spec__ ’: None,’ __builtins__ ’: & lt-in module’ )ins’ (built , ’__package__’: None, ’__loader__’:% lt; _frozen_importlib_external.SourceFileLoader object at 0x7f88 5e463470"}
This is using globals (): {’__file__’: ’/home/34dde64e1e47944021cdf478b97f13a0.py’, ’__doc__’: None, ’__name__’: ’__main__’, ’__cached__’: None ’: None,’ __builtins__ ’: "module’ builtins’ (built-in) & gt ;, ’__package__’: None, ’__loader__’: "_frozen_importlib_external.SourceFileLoader object at 0x7f885e463470 & gtlock;}

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method