Skip to main content

16. Class Vs Static Methods

Python provides @classmethod and @staticmethod for alternative method definitions.


Class Method Example

class Person:
count = 0
def __init__(self, name):
Person.count += 1
self.name = name

@classmethod
def total(cls):
return cls.count

print(Person.total()) # 0
p = Person("Alice")
print(Person.total()) # 1

Static Method Example

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

print(Math.add(2, 3))

Wrap-Up

  • Class methods access class state.
  • Static methods are independent utility functions inside a class.