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
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.

After this program

If the idea is still fuzzy, read Day 11: Classes and Objects in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 object-oriented programs, or see every topic on Python exercises.