Skip to the solution
MediumDSA6 min read

What is the difference between classmethod and staticmethod?

790 views

The problem

Question presented to candidate: "Python has @classmethod and @staticmethod. What is the difference between them and a normal method, when would you reach for each, and what actually happens when you call one?"

What a strong answer should cover:

  • A normal method receives the instance (self), a class method receives the class (cls), a static method receives nothing extra β€” it is a plain function namespaced in the class.
  • Class methods are the tool for alternative constructors (dict.fromkeys, datetime.fromtimestamp) because cls(...) builds the subclass when called on a subclass; a static method has to hard-code the class.
  • Both can be called on the class or an instance; a class method called through an instance still gets the class.
  • Mechanism: both are descriptors stored in the class __dict__; classmethod.__get__ returns a method bound to the class, staticmethod.__get__ returns the underlying function unchanged.
  • Version facts: since Python 3.10 a staticmethod object is itself callable and both expose __wrapped__; chaining @classmethod over @property was deprecated in 3.11 and removed in 3.13.
  • Choosing: use a static method for a helper that needs neither the instance nor the class (or a module-level function), a class method when the class itself matters.

Code / implementation expected: Yes β€” a class with all three method kinds, an alternative constructor via @classmethod, and a subclass showing the difference.

  • classmethod
  • staticmethod

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

PythonInstance, class and static methods; alternative constructors and subclasses
PythonDescriptor view: __dict__ objects, __get__, __func__ and per-class state

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 66 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextMedium

How does exception propagation work up the call stack?

Continue