Static Method

What is a static method in Python?

Answer

Static methods are functions namespaced inside a class which do not automatically receive self or cls. This makes them plain functions inside a class, and not methods.

This is made possible through the @staticmethod decorator, which turns the function below into a static method by disabling the automatic passing of the instance or the class.

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

m = Math()
print(m.add(3, 4))     # 7 (no self or cls passed)
Back to collection