Skip to content

Module 11 · Object-Oriented Programming — topic 13 of 25

Static methods

Methods with @staticmethod that receive neither self nor cls: utility functions grouped with a class, validation helpers, and when a plain module function is better.

Beginner 8 min read·Lesson 170 of 306 in Python Programming

Learning objectives

  • Define and call static methods
  • Choose between instance, class and static methods
  • Recognise when a module-level function is clearer

Definition

A static method is defined with the @staticmethod decorator and receives no automatic first argument. It behaves like a normal function that lives in the class's namespace.

Introduction

Some helpers relate to a class but don't need any object or class data: checking whether a PIN is well formed, converting units. Static methods keep such helpers next to the class that uses them.

Why it matters

Knowing the three method types — instance, class and static — lets you pick the one that states your intent: 'needs the object', 'needs the class', 'needs neither'.

Detailed explanation

Decorate with @staticmethod; there is no self or cls parameter.

Call through the class (Account.is_valid_pin('1234')) or an instance; both work.

Static methods are useful for validation helpers and pure computations that conceptually belong to the class.

Summary: instance methods use self (object data); class methods use cls (class data or construction); static methods use neither.

If a helper isn't specifically about the class, a module-level function is simpler and easier to reuse.

Syntax

Python
class Account:
@staticmethod
def is_valid_pin(pin):
return pin.isdigit() and len(pin) == 4
Account.is_valid_pin("1234")
  • No self or cls.
  • Call on the class or an instance.
  • Use for helpers related to the class.

Code examples

A validation helper

Python
class Account:
@staticmethod
def is_valid_pin(pin):
return pin.isdigit() and len(pin) == 4
for pin in ["1234", "12a4", "12345"]:
print(pin, Account.is_valid_pin(pin))
Output
1234 True
12a4 False
12345 False

Three kinds of method

Python
class Temperature:
unit = "C"
def __init__(self, value):
self.value = value
def show(self): # instance method
return f"{self.value}{self.unit}"
@classmethod
def freezing(cls): # class method
return cls(0)
@staticmethod
def to_f(c): # static method
return c * 9 / 5 + 32
print(Temperature(25).show(), Temperature.freezing().show(), Temperature.to_f(25))
Output
25C 0C 77.0

More examples

Static helpers used by instance methods

Python
class Order:
GST = 0.18
def __init__(self, items):
for name, price, qty in items:
if not Order.valid_line(price, qty):
raise ValueError(f"bad line: {name}")
self.items = items
@staticmethod
def valid_line(price, qty):
return price > 0 and isinstance(qty, int) and qty > 0
@staticmethod
def money(amount):
return f"Rs {amount:,.2f}"
def total(self):
subtotal = sum(p * q for _, p, q in self.items)
return self.money(subtotal * (1 + self.GST))
print(Order([("pen", 20, 3), ("bag", 1200, 1)]).total())
print(Order.money(1234567.8))
try:
Order([("pen", 20, 0)])
except ValueError as e:
print(e)
Output
Rs 1,486.80
Rs 1,234,567.80
bad line: pen

Step-by-step explanation

  • valid_line and money need no object data, so they are static.
  • The constructor and total call them through the class or self — both work.
  • money is also useful on its own, called as Order.money(...).

Expected output

Running “Static helpers used by instance methods” prints exactly:

TextOutput
Rs 1,486.80
Rs 1,234,567.80
bad line: pen

Practical applications

  • Format and parse helpers grouped with a domain class.
  • Validation rules checked before creating objects.

Best practices

  • Use static methods sparingly for closely related helpers.
  • Prefer module functions for general utilities.
  • Choose the method type that states what data is needed.

Common mistakes

Accessing self in a static method

Avoid

@staticmethod
def total(self): ...

Prefer

def total(self): ... # make it an instance method

Static methods don't receive the instance; if you need it, use an instance method.

Important notes

  • Static methods are common in other languages because they lack module-level functions; in Python, modules often serve that purpose.

Key takeaways

  • @staticmethod methods receive no self or cls.
  • They group related helpers with a class.
  • Instance → object data; class → class data; static → neither.

Try it yourself

  1. 1
    Add a static method is_valid_email to a User class and use it in __init__.
  2. 2
    Convert an instance method that doesn't use self into a static method.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • ModerateWhat is printed?
Practise this topic