Introduction

If you're already familiar with object-oriented programming in Python, you know that classes are an essential part of the language. A class creates a blueprint for objects, which are instances of the class itself. One of the most important concepts to understand when working with classes in Python is class variables. Class variables are variables that belong to the class and not to specific instances of the class. In this tutorial, we will explore what class variables are, how to create them, and how to modify them. We'll also provide some examples to demonstrate their usefulness. So let's get started!

Table of Contents :

  • Class variables
  • Accessing class variables
  • Modifying the class variables
  • Class variables in case of inheritance

Class variables :

  • A class variable is a variable that is attached to the class itself.
  • A class variable defines the state of the class itself.
  • A class variable is shared by all the objects of the class.
  • The value of the class variable remains the same for all the objects.
  • A class variable is declared inside a class but outside the  __init__  method or any instance method.
  • A class variable is also known as static variable.

Accessing class variables :

  • Class variables can be accessed using the class name as well as the object name.
  • But it is recommended that we access the class variables through class name only.
  • We can access a class variable at following places within our code :
    • inside a constructor
    • inside an instance method or
    • outside the class
  • The class variables can be accessed inside a constructor or an instance method using either the  self  keyword or class name.
  • The class variables can be accessed outside the class by using either the object reference or class name.
  • Code Sample : 

class Employee:
    company_name = "ABC Inc."

    def __init__(self, emp_name, emp_id, emp_desig):
        self.name = emp_name
        self.id = emp_id
        self.desig = emp_desig
        self.cname = Employee.company_name  # Accessing class variable using class name

    def print_details(self):
        print(f"Name of employee = {self.name}")
        print(f"ID of employee = {self.id}")
        print(f"Designation of employee = {self.desig}")
        print(f"Company of employee = {self.company_name}")  # Accessing class variable using self keyword
        print()


# Creating objects
emp_1 = Employee("Steve", "k2591", "Manager")
emp_2 = Employee("Rob", "k2586", "Tech Lead")

# Accessing class variable outside the class
# using class name
cmp_name = Employee.company_name
print(f"Employee details for {cmp_name}")
print()

emp_1.print_details()
emp_2.print_details()


# Accessing class variable outside the class
# using object name
emp_1_cname = emp_1.company_name
print(f"Company name for Employee 1 = {emp_1_cname}")


# Output
# Employee details for ABC Inc.

# Name of employee = Steve
# ID of employee = k2591
# Designation of employee = Manager
# Company of employee = ABC Inc.

# Name of employee = Rob
# ID of employee = k2586
# Designation of employee = Tech Lead
# Company of employee = ABC Inc.

# Company name for Employee 1 = ABC Inc.


Modifying the class variables :

  • Class variable can be modified using the dot notation and class name.
  • For modifying the class variables we should use class name only.
  • Note # : If we use object name to modify a class variable, a new instance variable of the object is created which shadows the class variable.
  • Code Sample : 

class Employee:
    company_name = "ABC Inc."

    def __init__(self, emp_name, emp_id, emp_desig):
        self.name = emp_name
        self.id = emp_id
        self.desig = emp_desig

    def print_details(self):
        print(f"Name of employee = {self.name}")
        print(f"ID of employee = {self.id}")
        print(f"Designation of employee = {self.desig}")
        print(f"Company of employee = {self.company_name}")
        print()


emp_1 = Employee("Steve", "k2591", "Manager")

# Printing initial details :
print(f"Initial Employee details for {Employee.company_name}")
print()
emp_1.print_details()


# Modifying class variable
Employee.company_name = "XYZ Inc."


# Printing modified details :
print(f"Modified Employee details for {Employee.company_name}")
print()
emp_1.print_details()



# Output
# Initial Employee details for ABC Inc.

# Name of employee = Steve
# ID of employee = k2591
# Designation of employee = Manager
# Company of employee = ABC Inc.

# Modified Employee details for XYZ Inc.

# Name of employee = Steve
# ID of employee = k2591
# Designation of employee = Manager
# Company of employee = XYZ Inc.


Class variables in case of inheritance :

  • When we inherit one class from another class, we can change the class variables of the parent class from the child class.
  • To change the value of the parent class's class variable we can use either the parent class name or the child class name.
  • Note # : If both parent and child classes have class variable with same name, then the child class will not inherit this class variable from the base class.
  • Code Sample : 

class Employee:
    company_name = "ABC Inc."

    def __init__(self, emp_name, emp_id, emp_desig):
        self.name = emp_name
        self.id = emp_id
        self.desig = emp_desig


class ContractEmployees(Employee):
    contract_term = "6 months"

    def print_details(self):
        print(f"Name of employee = {self.name}")
        print(f"ID of employee = {self.id}")
        print(f"Designation of employee = {self.desig}")
        print(f"Company of employee = {self.company_name}")
        print(f"Contract Term = {self.contract_term}")
        print()


emp_1 = ContractEmployees("Steve", "k2591", "Manager")
emp_1.print_details()

# Changing class variable of Parent class
# using child class name
ContractEmployees.company_name = "ABC Contract Inc."
emp_1.print_details()

# Ouput
# Name of employee = Steve
# ID of employee = k2591
# Designation of employee = Manager
# Company of employee = ABC Inc.
# Contract Term = 6 months

# Name of employee = Steve
# ID of employee = k2591
# Designation of employee = Manager
# Company of employee = ABC Contract Inc.
# Contract Term = 6 months



Prev. Tutorial : Instance variables

Next Tutorial : Instance methods