Declare Global Variable in Python Class



A global variable is a variable with global scope, meaning it is visible and accessible throughout the program. The collection of all global variables is known as the global environment or global scope of the program. The variables declared outside the function are, by default, global variables.

We use the global keyword before a variable inside a function or method to indicate that we are referring to the global variable rather than creating a new local one. The following is the syntax to declare a global variable inside a function -

global variable

Usage of Global Variable

Let's understand how to use the global variable in a function. A global variable is declared outside the function and is accessible both inside and outside the function. We can also modify the global variable inside the function.

Example

In the following example, we have defined the global variable side, and it is accessed directly inside the function Area(), and when we called the function it resulted the area of the square -

side=8

def Area():
   print("side of the square:",side)
   print("Area of the square:",side*side)

Area()

Following is the output of the above code ?

side of the square: 8
Area of the square: 64

Example - Modifying the 'global' variable

In the following example, we have modified the global variable inside the function -

side=8 #global variable

def Area():
   global side 
   side=10  #modified global variable inside the function
   print("side of the square:",side)
   print("Area of the square:",side*side)

Area()

Following is the output of the above code ?

side of the square: 10
Area of the square: 100

Declaration of Global Variable in class

A global variable that is declared inside the class can be accessed inside and outside the class, and it can be modified within the class using the global keyword.

Example

Here, we have declared two global variables, side1=15 and side2=12, which are accessible inside the class, Perimeter(). An object, Obj1 is created for instance of the Perimeter() class -

side1=15   #global variable 1
side2=12   #global variable 2

class Perimeter:
    def Square(self):
         print("Perimeter of the square:",4*side1)

Obj1=Perimeter()
Obj1.Square()

Following is the output of the above code -

Perimeter of the square: 60

Modifying the global variables in the class

In a class, we can modify the global variables using the global keyword inside the method. And changes made to the global variable inside the class will persist outside the class as well.

Example

Here, we have modified the global variable, side1. Initially, it was 15, in the class we have modified it to 10 using global keyword -

side1=15   #global variable 1
side2=12   #global variable 2

class Perimeter:
   def Square(self):
      global side1
      side1=10
      print("Perimeter of the square:",4*side1)

Obj1=Perimeter()
Obj1.Square()

Following is the output of the above code -

Perimeter of the square: 40

Class variables Vs Global variables

The following are the differences between the class variables and the global variables -

Class Variables Global Variables
The variables that are defined within and outside the methods of the class are known as class variables. The variables that are defined outside the functions and classes are known as global variables
These variables are accessed within the class and its derived class only These variables are accessible to all functions and classes
These can be modified within the class and its derived classes These variables can be modified with the class or functions by using the global keyword.

Example

In the following example, we can find the difference between the class variables and global variables -

side1 = 15   # Global variable 1
side2 = 12   # Global variable 2

class Perimeter:
    class_var="Sum of all sides of the square is perimeter"  #class variable
    def Square(self):
        global side1
        side1 = 10  # Modifies the global variable
        
        print("Perimeter of the square:", 4 * side1)

Obj1 = Perimeter()
Obj1.Square()
print(Obj1.class_var)

print("Updated value of side1:", side1)

Following is the output of the above code -

Perimeter of the square: 40
Sum of all sides of the square is perimeter
Updated value of side1: 10
Updated on: 2025-04-30T17:50:03+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements