Python classmethod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, @classmethod marks a method that receives the class itself as its first argument, conventionally named cls, instead of an instance (self). You can call it on the class, Date.from_string(...), or on an instance.

The classic use is an alternative constructor: a method that parses some input and returns cls(...). Because cls is whichever class the method was called on, subclasses automatically get instances of the subclass.

classmethod is a built-in type (a class) that you call like a function: each call creates a new classmethod object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Usually written as a decorator.

python
class MyClass:
    @classmethod
    def create(cls, data):
        return cls(data)
CallWhat it does
MyClass.create(x)Called on the class.
obj.create(x)Also works; cls is still the class.
SubClass.create(x)cls is SubClass.
classmethod(func)The non-decorator form.

Use a class method when the method needs the class (to construct objects or read class attributes) but not a particular instance.

Library declarations
python
classmethod(function, /)
# class classmethod: usually used as @classmethod

Parameters

ParameterDescription
functionRequired. A function whose first parameter will receive the class.

Return Value

Return ValueDescription
classmethodA class method object to store in the class body.

Python Examples

Example 1 — Build Objects from a String

Add a from_string() constructor next to the normal __init__.

python
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)

    def __repr__(self):
        return f"Date({self.year}, {self.month}, {self.day})"


d = Date.from_string("2024-03-09")
print(d)
print(d.month + 1)

How It Works

  1. cls receives Date. Calling Date.from_string passes the class as cls.
  2. Parse, then construct. The text is split, converted with int(), and passed to cls(...).
  3. Real ints. The month is the number 3, so adding 1 gives 4.
  4. Clean repr. __repr__ makes the printed object readable.

Example 2 — cls Is the Class You Called It On

Call the same class methods on a parent class and a subclass.

python
class Shape:
    @classmethod
    def create(cls):
        return cls()

    @classmethod
    def describe(cls):
        return "I am " + cls.__name__


class Square(Shape):
    pass


print(Shape.describe(), Square.describe())
print(type(Square.create()).__name__)
print(Square().describe())

How It Works

  1. Inherited methods. Square does not define anything, yet it has both class methods.
  2. cls changes. In Square.describe(), cls is Square, so the name differs.
  3. Right type created. Square.create() returns a Square, not a Shape.
  4. Instances work too. Calling through an instance still passes its class as cls.

Example 3 — Shared Counters and the classmethod() Call

Track issued tickets at class level and attach a class method without the decorator.

python
class Ticket:
    issued = 0

    def __init__(self):
        Ticket.issued += 1
        self.number = Ticket.issued

    @classmethod
    def reset(cls):
        cls.issued = 0


def total(cls):
    return cls.__name__ + ": " + str(cls.issued)


Ticket.total = classmethod(total)

first, second = Ticket(), Ticket()
print(first.number, second.number, Ticket.total())
Ticket.reset()
print(Ticket.total(), Ticket().number)

How It Works

  1. Class attribute. issued is shared by all tickets and counts them.
  2. Reset via cls. cls.issued = 0 changes the class attribute, not one instance.
  3. Without the decorator. classmethod(total) wraps a plain function, which is then attached to the class.
  4. Left to right. In the last print, Ticket.total() runs before the new ticket is created, so it shows 0.

Frequently Asked Questions

A class method receives the class as cls; a static method receives nothing automatically.
The conventional name for the class passed to a class method, like self for instances.
For alternative constructors (from_string, from_dict) and for methods that work with class-level data.
No. It has no self. Create or receive an instance if you need instance data.
Yes. Python passes the instance's class as cls.

Next: compile()

Turn source text into a code object.

compile() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful