Can access instance attributes (self.attr) and
class attributes.
Called on an object.
class Example:
def instance_method(self):
return f"Called instance_method from {self}"
e = Example()
print(e.instance_method()) #works
# Output: Called instance_method from <__main__.Example object at 0x...>
Use when you need to work with data tied to a specific object.
2. Class Method
Defined with @classmethod decorator.
Takes cls (class itself) as the first parameter.
Cannot access instance-specific data (self), but
can modify class-level data.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
1. Instance Method (default)
selfas the first parameter.self.attr) and class attributes.Use when you need to work with data tied to a specific object.
2. Class Method
@classmethoddecorator.cls(class itself) as the first parameter.self), but can modify class-level data.Use when you need methods that work with class-level state (shared across all instances), or when you want alternate constructors.
3. Static Method
@staticmethod.selforcls.Use when the method doesn’t need instance (
self) or class (cls) data, but logically belongs to the class.Quick Comparison
self)cls)selfclsReal-world Analogy
Think of a Bank Account class:
deposit→ Needs to update that account’s balance → instance method.set_interest_rate→ Affects all accounts (class-level) → class method.validate_amount→ Just a check, doesn’t depend on account or bank → static method.Output -