The @classmethod decorator turns a method into one that receives the class as its first argument (conventionally cls) instead of an instance. You call it on the class itself or on any instance, and it can read or change class-level state. Its most common job is acting as an alternative constructor, also called a factory method.
@classmethod worksclassmethod versus staticmethod versus instance methods@classmethod with other decorators@classmethod worksA normal method receives the instance (self). A class method receives the class that it was called on:
cls.interest_rate = rate changes the attribute on the class, so every instance that does not override it sees the new value. No instance is required to call the method.
The best reason to use @classmethod is to offer several ways of creating objects without a long __init__:
Returning cls(...) rather than hard-coding Person(...) matters. It lets subclasses inherit the factory and get an instance of their own type:
The standard library uses this pattern widely, for example dict.fromkeys() and datetime.fromtimestamp().
classmethod versus staticmethod versus instance methods| Kind | First argument | Can reach instance data | Can reach class data | Typical use |
|---|---|---|---|---|
| Instance method | self | Yes | Yes, via self.__class__ | Behaviour of one object |
@classmethod | cls | No | Yes | Factories, class-wide settings |
@staticmethod | None | No | Only by naming the class | Helper that needs neither |
Use @staticmethod when the function does not need the class or instance but belongs with the class logically. Use @classmethod when it needs the class, especially for inheritance-friendly construction.
Class methods are handy for shared configuration, but assigning through cls affects the class where the attribute lives, and subclasses can shadow it:
The first call changes Base.count to 1. The second call reads 1 through inheritance and creates a new Child.count equal to 2, so the two counts now differ. If you want one shared counter, write Base.count += 1 explicitly. Mutable shared state also needs locking when threads are involved.
@classmethod with other decoratorsPython 3.9 let @classmethod wrap other descriptors such as @property. That was deprecated in 3.11 and removed in Python 3.13, so do not combine them in new code. For a class-level computed value, use a plain class method that you call with parentheses, or a metaclass property.
Class methods also suit registries and configuration loaders, where behaviour depends on the class being used:
Each factory names its input source, which reads better than one constructor with many optional arguments.
cls and self?self is the instance the method was called on. cls is the class, which may be a subclass of where the method was defined. Both are only naming conventions.
Yes. obj.method() still passes type(obj) as cls.
@classmethod instead of a module function?It keeps the factory next to the class and supports inheritance, since cls(...) builds the right subclass.
self?No. It has no instance. Create one with cls(...) if you need it.
@classmethod passes the class as cls. Use it for alternative constructors and class-wide behaviour, return cls(...) so subclasses work, and choose @staticmethod when neither the class nor an instance is needed.