Python Built-in Function globals():

The globals() is an inbuilt function in Python that returns the dictionary of the current module namespace.

In other words, this function returns all necessary information about the program’s global scope, including variable names, methods, classes, etc. So, the global symbol table is accessed in Python using this method. Moreover, this function can also be used to access the global variable inside a class or a method.

Signature

The signature for the globals() function is as shown below. Here, d is a dictionary that will be returned by this function.

d=globals()

Parameters and return type

  • This method doesn’t take any parameters.
  • However, it returns a dictionary containing the global scope of the program.

Python Built-in Function globals Examples:

Example 1: For example, let us initialize a variable, a list, and a set with some numbers. Thereafter, we will call the globals function. As a result, it returns the dictionary containing the name of a file, a package, cached data, loader along with the variable, list, and set.

#Initializing
a=5
l=[1,2,3]
s={11,22,33}
#Using globals and printing
print(globals())

Output

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fb09591fd90>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'main.py', '__cached__': None, 'a': 5, 'l': [1, 2, 3], 's': {33, 11, 22}}

Example 2: In this example, we will take the global variables “a” and “b”. Thereafter, we will write a function where we will modify the value of global variables. However, if we do not use the globals function to access the variables, it will consider it as a local variable for a function.

#Initializing
a = 5
b=50  
#Defining Function
def func():
    c = 10
    d = c + a
    # Calling globals()    
    globals()['b']=globals()['b']+100
    globals()['a'] = d
print("Before function calling a is ",a)
print("Before function calling b is ",b)
func()
print("After function calling a is ",a)
print ("After function calling b is",b)

Output

Before function calling a is  5
Before function calling b is  50
After function calling a is  15
After function calling b is 150

Example 3: In this example, We will take a student class and a method inside a class. Now, let us update the variable inside the function. Afterwards, we will create an instance of a class and make call to the function. As a result, it will update the value of a variable. So, once we are back after function execution, we print the modified value of a variable.

#Initializing
a=5
#Defining class and function
class student:
   def func(self):
		#Updating global variable in class
       globals()['a']=10
#Creating class instance and calling function
t=student()
t.func()
#Printing value of global variable
print(a)

Output

10

Conclusion

The globals() function returns a dictionary with a global namespace for the current program.

References

Happy Learning 🙂