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

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically