Some text some message..
Back Comparison: Instance , Class , and Static Method in Python 22 Aug, 2025

🌈 Python Methods Comparison

🔹 1. Instance Method

  • Works with self (object instance)

  • Can access/modify instance variables

  • Needs object to call

class Demo:
    def instance_method(self, x):
        return f"Instance Method: {x}"
        
obj = Demo()
print(obj.instance_method("Hello"))  # ✅ Works

🔹 2. Class Method

  • Works with cls (class itself)

  • Can access/modify class variables

  • Decorated with @classmethod

class Demo:
    class_var = "I am Class Variable"
    
    @classmethod
    def class_method(cls):
        return f"Class Method: {cls.class_var}"
        
print(Demo.class_method())  # ✅ Works without object

🔹 3. Static Method

  • No self, no cls

  • Acts like a normal function inside class

  • Decorated with @staticmethod

class Demo:
    @staticmethod
    def static_method(a, b):
        return f"Static Method: {a + b}"
        
print(Demo.static_method(5, 7))  # ✅ Works without object

🌟 Side-by-Side Comparison

Feature Instance Method Class Method Static Method
Decorator None @classmethod @staticmethod
First Argument self cls None
Access Instance Vars ✅ Yes ❌ No ❌ No
Access Class Vars ✅ Yes ✅ Yes ❌ No
Call via Object ✅ Yes ✅ Yes ✅ Yes
Call via Class ❌ No ✅ Yes ✅ Yes

In short:

  • Use Instance Method → when working with objects.

  • Use Class Method → when working with the class itself.

  • Use Static Method → when logic is independent of class & object.