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
Question 1

A method decorated with @staticmethod is called as Model.helper(x). What is not injected automatically?

Hint

Static methods are plain callables.

Question 2

You need DatasetConfig.from_yaml(path) that reads a file and returns an instance. Which decorator typically receives the class as first argument?

Hint

Alternative constructors often need cls.

Question 3

Minimal Gadget class: @staticmethod def greet() returns exactly 'hello'.

Hint

staticmethod takes no self.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Defensive APIs: validation, sanitization & exceptions
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Pavel
Pavel