4 Examples to Use Python globals() Function

In python, we have discussed many concepts and conversions. But sometimes, we come to a situation where we need to return the dictionary according to the current global symbol table. In this tutorial, we will be discussing the concept of the python globals() function, which is used to updates and returns a dictionary of the current global symbol table.

What is globals() function?

Python globals() function is a built-in function used to update and return a dictionary of the current global symbol table. The functions and variables that are not associated with any class or function are stored globally. It is mostly used for debugging purposes to see what objects the global scope actually contains.

Symbol Table: A symbol table is a type of data structure maintained or created by a compiler used to store all necessary information about the program. These include all the variable names, methods, classes, etc.

Global Symbol Table: The table is used to store all the information needed for the program’s global scope, and this information can be accessed using the built-in globals() function.

Syntax

The global table dictionary is the dictionary of the current module where inside a function, this is a module where it is defined, not the module where it is called.

globals()

Parameter

It does not take any parameter.

Return Value

It returns the dictionary associated with the current Global symbol table.

Examples Demystifying Python globals() Function in Python

Here, we will be discussing all the examples of the built in globals() function in python:

1. Using Globals() function in Python

In this example, we will try to print the given function directly with the help of the print function in python. globals() function will directly paste the Local Symbol Table information on the console, on that scope where globals() was called from!. Let us look at the example for understanding the concept in detail.

#print() function and globals() function
print(globals())

Output:

Using Globals in Python

Explanation:

  • Here, we will directly print the given function with the help of the print function.
  • The given function will directly paste the Global Symbol Table information on the console, on that scope where globals() was called from!
  • Hence, we obtain the required dictionary.

2. Python globals() function program with function

In this example, we will be using the global variable and then declare the function. Inside of the function, we will try to modify the value using the global variable. Then, we will call the globals() function and print the modified value. Let us look at the example for understanding the concept in detail.

# global variable
num = 15

def func():
	a = 10
	b = a + num
	
	# Calling globals()
	globals()['num'] = b
	print (b)
	
func()

Output:

Python globals() function program with function

Explanation:

  • Firstly, we will be taking the global variable as num equal to 15.
  • Then, we will be declaring a function with func().
  • Inside, which we have modified the value of some variable by using a global variable.
  • After that, we will be calling the globals() function.
  • At last, we will print the updated value of the variable of b.
  • Hence, you can see the modified value.

3. Modifying global variable using the function in Python

In this example, we will be using the global variable and then declare the function. Inside of the function, we will try to modify the value using the global variable. Then, we will call the globals() function and print the modified value. Let us look at the example for understanding the concept in detail.

#input number
number = 52

#applying globals() function
globals()['number'] = 25
print('The number is:', number)

Output:

 Modifying global variable using the function in python

Explanation:

  • Firstly, we will be taking the global variable as a number equal to 52.
  • After that, we will be calling the globals() function.
  • At last, we will print the updated value of the variable of number.
  • Hence, you can see the modified value of the number variable.

4. Using globals() inside a class in Python

In this example, the globals() function is used inside the class. We have assigned some integer values to the variable num. Then, we have called the class with the name python, and inside it printed the globals(). Let us look at the example for understanding the concept in detail.

num = 100
class python:
    print('\nglobals() value inside and outside class\n', globals())

Output:

Using globals() inside a class

Explanation:

  • Firstly, we have assigned an integer value to the num variable.
  • Then, we have called the class with the name python.
  • Inside the class, printed the globals().
  • Hence, we have seen the output.

Difference between locals(), globals() and vars() function in Python

locals() function

Python locals() function is an in-built function used to update and return a current local symbol table dictionary.

globals() function

Python globals() function is an in-built function used to update and return a current global symbol table dictionary.

vars() function

Python vars() function is an in-built function that is used to return a dictionary of the current namespace or the dictionary of argument. The main difference between vars() and locals() is that vars() can take an argument and return a dictionary for the requested object.

If we write the locals() function and globals() function in the global environment, they will result in the same output. So, we will be taking an example with the help of function.

def func():
    num = 31
    print ("Locals : ", locals())
    print ("Vars : ", vars())
    print ("Globals : ", globals())
func()

Output:

vars() function

Conclusion

In this tutorial, we have discussed the concept of the built-in globals() function in python. We have seen what the globals() function, symbol table, and global symbol table is. Then, we have explained to you the syntax, parameter, and return value of the function. At last, we have explained the concept in detail with the help of different examples. All the examples are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.

FAQs

1. What does globals () do in Python?

The globals() function in python is an in-built function that is used to return a dictionary of the current global symbol table. A symbol table contains all the important details about the program.

2. How to modify global variables using globals()?

We can easily modify the global variables with the help of the globals() function python. Let us see the code snippet through which you can understand the modification very nicely.

num = 20
globals()['num'] = 55
print('The number is:', num)

Output:

The number is: 55

As you can see in the code, the num variable is set with the value of 20. Then, we have applied the globals() function and changed the value of the num value to 55, and at last, you can see the output as 55. Through this, we can easily modify the global variable.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments