How to Use Static Class Variables in Python

In Python, we define a static variable inside the class but do not define it at the instance level. The changes we make on static variables at the class level will affect their value in all other class instances. The static variable is used when we do not want to change its value throughout our program.

This article will provide a detailed understanding of Python static class variables and how to use static class variables with multiple examples. The concepts listed below will be elaborated in this tutorial:

Let’s start with the usage:

How to Use Python Static Class Variables?

In Python, we declare class variables when we construct the class. Class variables are unique and shared between all the other objects of the class. We use a class variable when we want the same variables for all class instances. For example, if we want to include the name of a company in the company class, we use a static class variable because the company name is the same for all employees.

Let’s understand the basic concept of static class variables in Python:

Code

class Employee:
    employee_name = 0
 
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
        Employee.employee_name += 1
 
    def show(self):
        print('Employee Name:', self.firstname, self.lastname)
        print('Total Number of Employees:', type(self).employee_name)
 
s1 = Employee('Alex', 'Henry')
s1.show()
s2 = Employee('Lily', 'John')
s2.show()

In the above code:

  • A class name “Employee”  is initialized.
  • Static Class variable named “employee_name” is initialized within the class.
  • The “__Init__”constructor is used to access or define the class’s object and use the class’s attributes to access them and initialize them. Here, we created two attributes named “firstname” and “lastname
  • Self function is defined to show the output of the employee name and the total number of employees present.
  • We have changed the value of a class variable using the class name instances.

Output

The above output shows the name of the employees and the total number of employees.

How To Access Python Static Class Variables?

The class name is used to call the static variables in Python. There are also different techniques to access the class variables. Some of them are the following:

  • Using Constructor to access the Class Variable
  • Using Instance method to access the Class Variable

Example 1: Using Constructor to access the Class Variable

The following lines of code make use of the constructor to access class variables:

Code

class student:
    school='XYZ School'
    
    def __init__(self, name):
        self.name = name
        
        print(self.school)
        print(student.school)
        
z1 = student('Alex')

In the above code:

  • Class named “student” is initialized.
  • Class or static variable named “school” is defined inside the class.
  • In the “__init__” constructor, we are first accessing the class variable using self as “print(self.school)”. After that, the variable is accessed using the Class name “print(student.school)”.

Note: The object created in the code is necessary to define. Because the attributes of the “__inint__” constructor is initialized using this object.

Output

The above output shows the value of the static variable, accessed using the constructor’s “self” parameter and with the class name directly.

Example 2: Using the Instance method to access the Class Variable

Let’s experience the following code to access the class variable:

Code

class company:
    company_name ='qtryey'
    def __init__(self, name, id_no):
        self.name = name
        self.id_no = id_no
        
    def show(self):
        print('Using inside instance method')
        
        print(self.name, self.id_no, self.company_name)
        
        print(company.company_name)
        
z1 = company('Alex', 20)
z1.show()

print('we can also access from outside the class')
print(z1.company_name)
print(company.company_name)

In the above code:

  • Class name “company” is created.
  • Static Variable name “company_name” is created inside the class.
  • In the constructor function, we first initialized three attributes named “self”, “name” and “id_no”.
  • We define another function named “show()”for calling the static variable.
  • The static variable accesses using “self”and “class name”inside the constructor function with an instance method.
  • The class variable is accessed from outside of the class using the object reference method with the python statement “print(z1.company_name)” and with the class name “print(company.company_name)”.

Output

In the above output, the static variable is accessed using instance and class name.

That’s all from this guide!

Conclusion

In Python, we use static class variables when we want the same variables among all other class instances. Every class instance can access the static variables through the class name but can not change them. This post defines the methods to create and access the static class variables. For this, we created the static variable inside the class and accessed it using the instance method, the constructor function, and by class name.