Static Methods in Python

Use @staticmethod for utility methods that logically belong to the class but do not use self or cls.

BeginnerObject-Oriented ProgramsExample 14 of 25
static-methods.py
Run in browser
1# Program to demonstrate static methods
2
3class MathUtil:
4 @staticmethod
5 def is_even(n):
6 return n % 2 == 0
7
8print(MathUtil.is_even(4))
9print(MathUtil.is_even(7))

Output

True
False

What's going on

Static methods are namespaced inside the class but behave like plain functions without accessing instance or class state.