Python Global Keyword With Examples

The global keyword is a keyword that is used to declare a variable as a global. This keyword allows users to change or modify the variable which is currently declared outside of the scope. We can use a global keyword inside a function also if we want to change or modify variables.

Rule of 'global' keyword:

  • The Global keyword is used to define a variable inside the function so that it can be modified according to requirements.
  • If a variable is not declared with a global keyword inside the function, it works like a local variable by default.
  • If a variable is declared outside of the function, it works like a global variable by default.
  • It is not required to declare a variable outside of a function using a global keyword, because a variable outside of a function is always a global variable by default.

Use of 'global' keyword:  In Python programming language, there are different types of scopes are available. Different types of variables are declared in different scopes. Depending on the declaration of the variable, the scope of variables is decided. These variables may be or may not be accessible within the function. In some cases, modification of a variable becomes needed inside a function whose current scope is outside of the function. In such cases, the global keyword is used with the variable name.

Sample code without using the global keyword,

x = 2

def display():

    x = x+3 # Error will be generated

    print(x)

display()

Sample code using the global keyword,

x = 2

def display():

     global x

     x = x+3

     print(x) # It will print 5

display()

Use of global variables across the Python modules:

There are different ways available to use global variables across the modules. But one of the best ways is to create a special module and import this special module in all the modules of the application. In Python, only one object or instance is created for each module. If any changes are required, it can be easily done by using module objects the changes will be reflected in all the applications. The process of creating this special module is described below.

Process 1: Create a special module to store global variables and named it with the “.py” extension.

a = 0

b = 0.0

c = "null"

Process 2: Save this module as global.py.

Process 3: Create a Python file to modify or change the global variables and save the file as modify.py. In this file, the value of global variables will be modified.

import global

global.a = 5

global.b = 2.0

global.c = "Hello world"

Process 4: Create another Python file to modify and print the global variable.

import global

import modify

print(global.a)

print(global.b)

print(global.c)

Use of Global variable in Nested Functions:

In Python, a nested function is just like a nested loop. A function is first created and inside that function, another function is created using def. In this way, a nested function can be created. In the first function, more than one function can be created. The function in which another function is created is called the outer function. The functions which are created inside the outer function is known as an inner function. Using the global keyword, the variables declared in the outer function, can be accessed in the inner function. The global keyword has to use before the variable inside the nested function to declare the variable as a global. 

def system() : #outer function

      a = 10 # variable declared and initialized in outer function

      def display() # inner function

                global a # global keyword is used to change the value

                a = 25 # global variable a changed its value from 10 to 25

      display()

system()

print(a) # it will print 25