Static methods
A static method is an ordinary function that lives inside a class body. It receives no self and no cls, so it cannot touch instance or class state; it is grouped with the class only for readability.
Detailed Explanation
Write a function in a class body without @staticmethod and Python treats it as an instance method: calling it on an object passes that object as the first argument. Add @staticmethod and that implicit argument disappears. Luhn.is_valid("4539...") and Luhn().is_valid("4539...") both call the function with exactly the argument you wrote.
The honest summary is that a static method is a namespacing decision, not an object-oriented one. It says "this helper belongs conceptually with this class, and I want to find it here." Nothing more. A validation routine for card numbers, a pure conversion between two units used by the class, a small pure helper called by several methods — those are reasonable static methods.
This is where Python diverges sharply from Java and C#. In those languages, every function must live in a class, so static is the only way to write a plain function. Python has module-level functions. That means the honest default in Python is a module function, and @staticmethod needs a reason. The usual reason is discoverability: Temperature.to_kelvin reads better at the call site than a loose to_kelvin imported from somewhere.
A useful decision rule, in order. Does the function read or write instance attributes? Regular method. Does it need the class — to construct an instance, or to read class attributes that subclasses may override? Class method. Neither? Static method if it is tightly bound to the class's concept, module function otherwise.
The cost of choosing a static method over a class method is real and often overlooked. A static method is not polymorphic. It has no reference to the class it was called on, so a subclass that "overrides" it does not change what an inherited method calling SomeClass.helper() does — unless that call goes through self.helper() or cls.helper(), in which case normal attribute lookup applies and the override does take effect. If you can see yourself wanting a subclass to customise the helper, use a class method from the start.
Mechanically, staticmethod is a descriptor whose __get__ returns the underlying function unchanged, which is exactly why no argument gets bound. Two consequences follow. First, a static method is the cheapest of the three kinds at call time, because no bound-method object is created. The difference is small and not a reason to choose it. Second, since Python 3.10 a staticmethod object is itself callable, so patterns like storing one in a class-level dictionary of handlers finally work; on earlier versions that raised TypeError: 'staticmethod' object is not callable.
One more thing worth knowing for interviews: nothing stops you calling a static method on an instance, and nothing stops a static method from taking an argument named self. Neither is special-cased by Python. Both are confusing to read, so do neither.
Code Examples
class Card:
def __init__(self, number: str, holder: str):
self.number = number
self.holder = holder
@staticmethod
def is_valid_number(number: str) -> bool:
"""Luhn checksum. Needs no instance and no class -> static."""
digits = [int(d) for d in reversed(number)]
total = 0
for position, digit in enumerate(digits):
if position % 2 == 1: # double every second digit
digit *= 2
if digit > 9:
digit -= 9 # same as summing the two digits
total += digit
return total % 10 == 0
def masked(self) -> str:
"""Uses instance state -> ordinary method."""
return "*" * (len(self.number) - 4) + self.number[-4:]
print(Card.is_valid_number("4539578763621486")) # True, called on the class
print(Card.is_valid_number("1234")) # False
card = Card("4539578763621486", "A. Patel")
print(card.masked()) # ************1486
print(card.is_valid_number("1234")) # False; the instance is NOT passed inclass Pipeline:
"""Static vs class method, and why the difference bites."""
@staticmethod
def clean(text: str) -> str:
return text.strip().lower() # frozen at definition, not overridable
@classmethod
def normalise(cls, text: str) -> str:
return cls.clean(text) # looked up on cls -> subclass can win
class StrictPipeline(Pipeline):
@staticmethod
def clean(text: str) -> str:
return text.strip().lower().replace(" ", "-")
print(Pipeline.normalise(" Hello World ")) # hello world
print(StrictPipeline.normalise(" Hello World ")) # hello-world
print(Pipeline.clean(" Hello World ")) # hello world
# The override works only because normalise called cls.clean().
# Had normalise called Pipeline.clean() directly, both lines would print
# "hello world" and the subclass would look broken for no visible reason.Common Gotchas
Forgetting the decorator changes the meaning
a helper defined in a class body without `@staticmethod` is an instance method. `obj.helper("x")` then passes `obj` as the first parameter and you get a confusing `TypeError` about too many arguments.
Static methods are not polymorphic through the class
code that calls `Pipeline.clean(...)` by name ignores subclass overrides. Only calls routed through `self.` or `cls.` respect them. Reach for `@classmethod` when subclass customisation is plausible.
A module-level function is usually the better default
Python does not require a class to hold functions. If the helper is not tied to the class's concept, putting it at module level makes it easier to import, test, and reuse.
Callable-without-an-instance is new
before Python 3.10, unwrapping a `staticmethod` object from `SomeClass.__dict__` and calling it raised `TypeError`. On 3.10 and later it works, which matters for handler tables and decorator stacking.