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

ParameterDescription
function The function to convert (the decorator passes it for you).

Return value

A class method object.

Examples

An alternative constructorTry it yourself
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
cls follows the subclassTry it yourself
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

VersionChange
Python 3.0Added
Python 3.9Class methods could wrap other descriptors such as property.
Python 3.11Wrapping other descriptors was deprecated.
Python 3.13Wrapping other descriptors was removed.