Junior — Middle
Explain the purpose and usage of a static method decorator in Python.
sobes.tech AI
Answer from AI
A static method in Python is a class method that does not depend on an instance of the class and does not have access to instance or class attributes (no self or cls parameters). It is used to group functions that are logically related to the class but do not require access to its state.
To create a static method, the @staticmethod decorator is applied before the method definition:
class MathUtils:
@staticmethod
def add(a, b):
return a + b
# Call without creating an instance
result = MathUtils.add(5, 3) # 8
Such a method can be called directly through the class, which is convenient for utility functions related to the class in a logical sense but do not use its data.