Python  

What is the difference between class method, static method, and instance methods?

🔹 Introduction

In Python’s Object-Oriented Programming (OOP), methods are special functions that are written inside a class. These methods describe the behavior of a class or its objects. However, not all methods behave the same way. In Python, methods are mainly of three types:

  1. Instance methods – they deal with data related to a particular object.
  2. Class methods – they work with data shared by the entire class.
  3. Static methods – they work like normal functions inside a class and do not depend on either class data or object data.

Let’s understand each one in detail.

👤 Instance Methods

An instance method is the most common type of method in Python. It always takes self as the first parameter. The keyword self refers to the object (instance) that is calling the method.

Key Points:

  • Instance methods are used to access and modify object-specific data.
  • Each object can have different values, and instance methods work with those values.

Example:

class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    # Instance method
    def display(self):
        print(f"Name: {self.name}, Marks: {self.marks}")

# Usage
s1 = Student("Alice", 85)
s2 = Student("Bob", 90)

s1.display()  # Name: Alice, Marks: 85
s2.display()  # Name: Bob, Marks: 90

Here, the display method is an instance method. It uses self to access the data stored inside each student object.

🏛️ Class Methods

A class method works at the class level. Instead of working with a single object, it works with data that belongs to the class itself. Class methods take cls as the first parameter, which represents the class rather than the object.

Key Points:

  • Class methods are defined using the @classmethod decorator.

  • They are mostly used to modify or access class variables that are shared among all objects of the class.

Example:

class Employee:
    company_name = "TechCorp"

    def __init__(self, name):
        self.name = name

    @classmethod
    def change_company(cls, new_name):
        cls.company_name = new_name

# Usage
e1 = Employee("John")
e2 = Employee("Jane")

print(Employee.company_name)  # TechCorp
Employee.change_company("CodeWorks")
print(Employee.company_name)  # CodeWorks

Here, the change_company method is a class method. It changes the company_name for the entire class, which affects all employee objects.

⚡ Static Methods

A static method is like a normal function inside a class. It does not take self or cls as a parameter. This means it does not depend on either object data or class data.

Key Points:

  • Static methods are defined using the @staticmethod decorator.
  • They are used for utility tasks that logically belong to the class but don’t need access to object or class data.

Example:

class MathUtils:
    @staticmethod
    def add(a, b):
        return a + b

    @staticmethod
    def multiply(a, b):
        return a * b

# Usage
print(MathUtils.add(5, 10))      # 15
print(MathUtils.multiply(3, 4)) # 12

Here, add and multiply are static methods. They perform calculations but don’t use any class or object data.

⚖️ Detailed Comparison

Type First Parameter Works with Object Data Works with Class Data Example Use Case
Instance Method self ✅ Yes ✅ Yes (if needed) Access or update data of a specific object
Class Method cls ❌ No ✅ Yes Update or manage data shared by the class
Static Method None ❌ No ❌ No Perform helper tasks that do not depend on object or class data

🏁 Summary

In Python, methods inside classes can be of three types: instance methods, class methods, and static methods. Instance methods use self to work with data of specific objects. Class methods use cls to manage data shared across the entire class. Static methods are independent functions inside a class that don’t use either object or class data. Understanding these differences helps you write better, cleaner, and more organized Python programs.