Static methods versus instance misuse
Intermediate
Defensive APIs: validation, sanitization & exceptions
Created by Pavel
· 29.04.2026 at 19:10 UTC
@staticmethod functions live in class namespace but do not receive self or cls. They are plain functions namespaced for organisation (Vector.from_polar). @classmethod receives the class object as first argument—common for alternative constructors.
Confusion happens when instance methods are decorated incorrectly and callers expect implicit self binding—arity errors and TypeError at runtime.
Choosing between staticmethod, classmethod, and module-level functions is style and discoverability; semantics matter for frameworks (Pydantic validators, SQLAlchemy hooks).
staticmethod docs: [1].
Sources
University approvals: 0
Tasks
Card Info
- Topic: Defensive APIs: validation, sanitization & exceptions
- Difficulty: Intermediate
- Completed: 0 users
Creator
Pavel