Python Mastery – From Basics to Brilliance / Object-Oriented Programming (OOP)

Instance, Class and Static Methods

1️⃣ Instance Method

An instance method works with the data of a specific object.

It takes self as its first parameter.

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

    def greet(self):
        return f"Hello {self.name}"

student = Student("Codeveda")

print(student.greet()) #Hello Codeveda

Here, self refers to the current object.

🧠 Instance Method

self → Current object

Use it when the method needs to access or modify instance variables.


2️⃣ Class Method

A class method works with the class itself rather than a particular object.

It uses the @classmethod decorator and takes cls as its first parameter.

class Student:
    school = "ABC School"

    @classmethod
    def change_school(cls, name):
        cls.school = name

Student.change_school("XYZ School")

print(Student.school) #XYZ School

Here, cls refers to the class.

🧠 Class Method

cls → Class

Use it when the method needs to access or modify class variables.


3️⃣ Static Method

A static method doesn't need access to either the object or the class.

It uses the @staticmethod decorator.

class Calculator:

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

print(Calculator.add(10, 5)) #15

It doesn't use self or cls.



⚖️ Quick Comparison

Instance Method 👤

Class Method 🏫

Static Method 🛠️

Uses self

Uses cls

No self or cls

Works with object data

Works with class data

Independent of object/class data

Called using object

Can be called using class/object

Usually called using class

No decorator required

@classmethod

@staticmethod

⭐ Remember

Instance Method → self → Object 👤

Class Method    → cls  → Class 🏫

Static Method   → none → Independent 🛠️

Easy rule:

If you need the object → self
If you need the class → cls
If you need neither → @staticmethod