Methods and Functions

Python locals ()

Python locals () function is a built-in function in the Python directory. It is used to obtain the dictionary of the current local symbol table. The symbol table is the data structure that is used to store all the information required to execute any program. It is created by the compiler before the execution of the program. 

The symbol table is of two types. The first is a global symbol table. This symbol table stores all the information that is related to the global scope of the program. The information stored in the table is accessed using the globals () method.

The global scope will include all the functions and variables that are not associated with any particular class or function in the program.  

A local symbol table stores the information that is required by the local scope of the program. The information in the local symbol table can be accessed using the built-in function called locals ().  The local scope can be within a function or a class. 

Syntax of locals ()

To use the locals () method, the following syntax is followed in Python. 

locals ()

locals () Parameters 

No parameters are passed in the locals () method. That is, the method does not take any input parameter. 

Return Value from locals ()

The Python locals () function returns all the information that is stored in the local symbol table in the form of a dictionary. Thus, when used in a program, the method will return a dictionary that contains all the local scope variables.  

Example 1: How locals () works in Python?

                    

from pprint import pprint 

a = 10

b = 20

def func1 ():

              x = 30

              y = 40

pprint (“locals () = {0}”.format (locals ()))

pprint (locals ())

print (‘*’ * 80)

pprint (“locals () == globals ()? “, locals () == globals ())

print (‘*’ * 80)

func1 ()

Expected Output:

                    

{‘__builtins__’: <module ‘builtins’ (built-in)>,

‘__cached__’: None,

‘__doc__’: None,

‘__file__’: ‘module1.py’,

‘__loader__’: <_frozen_importlib_external.SourceFileLoader object at 0x7fa1870a828>,

‘__name__’: ‘__main__’,

‘__package__’: None,

‘__spec__’: None,

‘a’: 10,

‘b’: 20,

‘foo’: <function foo at 0x7fa1878752f0>,

‘pprint’: <function pprint at 0x7fa1878756a8>}

 

********************************************************************************

 

locals() == globals()? True

 

********************************************************************************

 

locals() = {‘y’: 40, ‘x’: 30}

You will note that for a global environment, the globals () and locals () methods provide the same information. This is because, for a global environment, the local and global symbol table contains the same values.   

Example 2: How locals () works inside a local scope?

The program below shows how locals () function works when used inside a local scope. 

                    

def locals1 ():

              return locals ()

def locals2 ():

              present = True

              return locals ()

print (‘Locals are not present:’, locals1 ())

print (‘Locals present:’, locals2 ())

Output:

                    

The program above should provide you with the following output. 

Locals are not present: {}

Locals present: {‘present’: True}

Thus, when the locals () function is used inside a local scope, then it will return a true value. 

Example 3: Updating locals () dictionary values

The locals () function cannot be used to modify the data that is present in the local symbol table. 

                    

def locals1 ():

              present True:

              print (present)

              locals () [‘present’] = False                         

              print (present)

locals1 ()

Output:

When the locals () function is used to update the values present in the local symbol table, then after updating, it will return True. Thus, the output is as given below. 

True 

True 

Note that the Python locals () dictionary may not change the information present inside the locals table whereas the globals () dictionary immediately reflects the changes that are made to the global symbols table. 

Frequently Asked Questions 

What are locals in Python?

Locals is a built-in function in Python that is used to obtain all the information that is stored in the local symbol table. The local symbol table is the dictionary created by the compiler that stores the information required by the compiler to execute any program. 

Does Python have local variables?

Yes, Python can have local variables. These variables are defined inside a function or class and can be used concerning that function or class only. 

What are local variables in Python?

Local variables in Python are the variables that are defined inside a block. These variables are available only inside that block and cannot be used outside it. For example, a variable that is declared inside a function will be called a local function.

What are globals in Python?

Global variables are those variables in Python that can be used globally in the entire program. These variables are defined outside any function block. Thus, the scope of such a variable is said to be global. The function globals () is used to access the information that is present inside the global symbol table. It is a data structure that stores all the information related to the global scope of the program and is necessary for the execution of the program. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.