Python 3 reference · Built-in Functions
Python classmethod() Function
Turn a method into a class method that receives the class (cls) instead of an instance. Usually written as @classmethod.
Python 3 Since 3.0
Syntax
@classmethod
def method(cls, ...): ...
Parameters
| Parameter | Description |
|---|---|
function |
The function to convert (the decorator passes it for you). |
Return value
A class method object.
Examples
class Date:
def __init__(self, year, month, day):
self.year, self.month, self.day = year, month, day
@classmethod
def from_string(cls, text):
year, month, day = map(int, text.split("-"))
return cls(year, month, day)
d = Date.from_string("2026-10-21")
print(d.year, d.month, d.day)
Output
2026 10 21
class Animal:
count = 0
@classmethod
def create(cls):
cls.count += 1
return cls()
class Dog(Animal):
pass
print(type(Dog.create()).__name__) # cls is Dog here
print(Animal.count, Dog.count)
Output
Dog 0 1
classmethod vs staticmethod
A class method receives the class as cls, so it works correctly in subclasses; it is ideal for alternative
constructors such as from_string(). A static method receives neither the class nor the instance.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
| Python 3.9 | Class methods could wrap other descriptors such as property. |
| Python 3.11 | Wrapping other descriptors was deprecated. |
| Python 3.13 | Wrapping other descriptors was removed. |
Related
staticmethod()· Turn a method into a static method that receives neither the instance nor the class. Usually written as @staticmethod.property()· Turn methods into a managed attribute with a getter, and optional setter and deleter. Usually written as @property.super()· Return a proxy that calls methods of the parent class, so a subclass can extend a method instead of replacing it.- Python Classes and Objects · tutorial