globals() and locals() Methods in Python with Examples

globals() and locals() Python

In this post, you are going to learn globals() and locals() in Python. To understand these two methods, first, we need to understand the Symbol table.

A Symbol table is a data structure that contains all the necessary information like variable names, methods, classes, etc.

There are two kinds of symbol tables:

  1. Local symbol table
  2. Global symbol table

The local symbol table contains the information related to the local scope (for example, within a function) of the program. You can access it using the locals() method. It returns the dictionary of the current local symbol table.

The global symbol table contains the information related to the global scope (for example, variables that are not associated with any function) of the program. You can access it using the globals() method. It returns the dictionary of the current global symbol table.

Both locals() and globals() are built in functions.

Example:

global1 = 100
global2 = 200
      
def fun1():
pass
      
print(globals())

Output:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':<_frozen_importlib_external.SourceFileLoader object at 0x0000019616994790>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'J:\\cool.py', '__cached__': None, 'global1': 100, 'global2': 200, 'fun1': <function fun1 at 0x0000019726573E20>}

In the above code, global1 and global2 are the two global variables and fun1() is a function. We have called globals() inside the print() method. The globals() returned a dictionary that contains the information about the variables and function name. You can see global1, global2, and fun1 in the output.

Similarly, let’s create some variables inside fun1() and call the locals() method.

global1 = 100
global2 = 200
      
def fun1():
     local1 = 1000
     local2 = 1000
     print(locals())
      
fun1()

Output:

{'local1': 1000, 'local2': 1000}

We have created local1 and local2 variables. They are inside fun1(), so local to fun1() only. We have called locals() in the print() method. The locals() method returned a dictionary that contains information about the local variables.

Real Usage of globals() Method:

In Python, we cannot directly modify global variables inside a function. One way to do it is to use the global keyword. The other way is using the globals() function.

Example:

message = 'Hi'

def new_message():
     globals()['message'] = 'Hi Hello'
      
new_message()
      
print(message)

Output:

Hi Hello

In the above code, the message is a global variable. Inside new_message(), we are assigning ‘Hi Hello’ to globals()[‘message’]. Since globals() returns a dictionary that contains the message variable, ‘Hi Hello’ will be assigned to the message variable. That is why the output is Hi Hello.

Note: In general, using the above two methods is not a good idea. It makes debugging difficult when you modify global variables like this. I just posted this to inform you that globals() and locals() methods exist in Python. Try to avoid these two methods in production.

Leave a Comment