staticmethod()
A plain function that lives inside a class. The compiler drops the auto-self / auto-cls behavior.
Common call
@staticmethod
def helper(x): ...
Returns
a function — no cls, no self
Replaces
a module-level function when the logical home is inside a class
Watch out
cannot access class or instance state; if you need cls, use classmethod
staticmethod(functionfunction — The function to wrap. When called via the class or an instance, no automatic first argument is prepended. Used as @staticmethod decorator syntax.type: callable · required)
→ staticmethod
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| function | callable | yes | The function to wrap. When called via the class or an instance, no automatic first argument is prepended. Used as @staticmethod decorator syntax. |
Return value
staticmethod — A descriptor that, when accessed on the class or an instance, returns the wrapped function UNBOUND. No implicit first argument is passed. Useful for utility functions that logically belong to the class's namespace but do not need the class or instance.
Common patterns
Utility inside a class
When the function is related to the class but does not need self or cls.
class ImagePath: def __init__(self, path): self.path = path @staticmethod def valid_extension(name): return name.lower().endswith((".png", ".jpg"))
Namespace grouping
Discoverability — related helpers live under one class.
class TextUtils: @staticmethod def slugify(s): ... @staticmethod def word_count(s): ...
When you don't need the class
If cls is unused, staticmethod is more honest about it than classmethod.
# BAD: @classmethod # def add(cls, a, b): return a + b # cls unused # GOOD: @staticmethod # def add(a, b): return a + b
Prefer a module-level function when possible
If the function has no relationship to the class beyond namespacing, module scope is often cleaner.
# module.py def slugify(s): ... # vs. class TextUtils with @staticmethod slugify
Examples
1. Basic use
ImagePath.valid_extension("photo.jpg")
Returns
True2. Via instance
p = ImagePath("x")
p.valid_extension("y.png")
Returns
True # no self passed3. No auto-argument
@staticmethod
def greet(name): ...
greet("Alice")
Returns
"hi Alice" # name is Alice, not the class4. Access from class
type(ImagePath.valid_extension)
Returns
<class 'function'>5. Cannot access cls
# @staticmethod cannot see cls or self
# use @classmethod if you need cls
Returns
Pitfalls
1. staticmethod cannot access the class or instance
This is the whole point — but new users sometimes want cls and reach for staticmethod anyway. If you need cls, use classmethod. If you need self, use a regular method.
No access
class C: x = 1 @staticmethod def f(): return x # NameError
NameError: name 'x' is not defined
Use classmethod
@classmethod def f(cls): return cls.x
works
2. Redundant when the function does not need the class namespace
If the function is not conceptually tied to the class, a module-level function is clearer. `class Utils: @staticmethod def add(a, b): ...` is a code smell — just define `def add(a, b): ...` at module level.
Over-scoping
class Utils: @staticmethod def add(a, b): return a + b Utils.add(1, 2)
3, but why the class?
Module-level
def add(a, b): return a + b add(1, 2)
3, cleaner
3. Not the same as a regular def in a class body
`def f(x): ...` inside a class becomes an unbound function on the class. Calling `C.f(1)` works, but `C().f()` passes the instance as x. staticmethod removes that instance binding.
Auto-self surprise
class C: def f(x): return x C().f()
the instance passed as x
staticmethod
class C: @staticmethod def f(x): return x C.f(1)
1
4. Called on an instance loses no information — no self anyway
A staticmethod called via `instance.method(...)` works exactly the same as `Class.method(...)`. There is no implicit first argument. Prefer calling on the class for clarity.
Confusing via instance
obj.static_helper(x)
works, but reads like a method
Call on class
Class.static_helper(x)
clearer
When to use
Use it
- Utility functions logically tied to a class's namespace
- Helpers used inside class methods that need no state
- Namespacing related helpers for discoverability
- When you would say "this belongs to the class, but not the instance"
Reach for something else
- You need cls → classmethod
- You need self → regular method
- The function has no class relationship → module-level def
- You want an alternative constructor → classmethod
Notes
Complexity
O(1) descriptor invocation
Return
The wrapped function, unbound
CPython impl
Objects/funcobject.c :: staticmethod_descr_get
Memory
Small descriptor object
Thread-safe
Yes for the wrapper; depends on the function
FAQ
classmethod passes the CLASS as the first argument (cls). staticmethod passes NOTHING implicit. Use classmethod when you need the class (alternative constructors, subclass-aware factories). Use staticmethod when you just want a function that lives in the class namespace.
History
2.2
staticmethod introduced with new-style classes.
3.10
staticmethod became callable directly (no longer requires attribute access first).