Python Static Methods

Static methods are functions defined in a class that do not have access to the class object or any instance of that class. Static methods are called using the class, not an instance of the class.

Python
class MyClass:

    @staticmethod
    def method():
        return 42
	
MyClass.method()  # Call the Static Method

A method is made static be adding the @staticmethod decoration. Static methods do not take self or cls as the first parameter.

Consider a mathematics class whose purpose is simply to collect together a set of methods for performing mathetical operations. You don't necessarily need to instantiate a mathematics object, nor does the class need to store any data. It's just a bunch of methods.

Python
class MathUtilities:

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

    @staticmethod
    def multiply(a,b):
        return a * b

You call the methods via the class name:

Python
print(MathUtilities.add(1, 2))    
print(MathUtilities.multiply(4, 5)) 

Output

3
20