Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Decorator

classmethod

@classmethod turns a method into one whose implicit first argument is the class. It may be called through a class or instance, and subclass calls receive the subclass.

How it works

Use @classmethod when a method should receive its class rather than an instance. Python supplies that class as the first argument, and calls through a subclass supply the subclass, enabling class-aware behavior and construction.

Syntax

@classmethod
def method(cls, arg1, arg2): ...

Parameters

NameTypeDescription
clstypeThe class object supplied implicitly by Python; conventionally named cls.
*args, **kwargsobjectAny explicitly declared arguments after cls.

Returns

The decorated method's return value; the page does not specify a separate return type.

Key points

  • @classmethod is a decorator that changes a method into a class method.
  • The decorated method receives its class automatically as the first argument, conventionally written as cls.
  • This parallels ordinary instance methods, which receive an instance implicitly.
  • Place @classmethod directly above the method definition inside the class body.
  • The decorator is applied to a function defined as a method.
  • Call it through the class or through an instance.
  • Subclass calls receive the subclass itself, preserving polymorphic class-aware behavior.

Common mistakes

  • Calling a class method through an instance does not use that particular instance; only its class determines the implicit argument.
  • Do not equate Python class methods with static methods from C++ or Java; the decorator supplies a class argument.

Example: Subclass-aware factory

class User:
    def __init__(self, name):
        self.name = name

    @classmethod
    def guest(cls):
        return cls("guest")


class Admin(User):
    pass

user = Admin.guest()
assert type(user) is Admin
assert user.name == "guest"

The decorator lets a factory method receive the class used for the call, so inherited class methods can construct the appropriate subclass.

When to use it

Class-aware factory methods

Construct an object from an alternate representation while preserving the calling subclass.

Class-dependent operations

Implement behavior that needs class-level context rather than a particular object.

Interview questions

QWhat does @classmethod do?

@classmethod transforms a method so its first implicit argument is the class, conventionally named cls. It can be called on either the class or an instance.

Follow-up: What happens through inheritance? The class object for the derived class is passed when the method is called on that derived class.

QHow can a class method be called?

A class method can be invoked as C.f() or C().f(). In the instance form, Python ignores the instance itself and uses its class for the implicit argument.

Follow-up: Can the method distinguish two instances? Not from the implicit argument alone, because the instance is not passed.

QWhy use cls instead of hard-coding a class name?

A class method receives the class as its implicit first argument, so it can implement behavior that depends on the calling class. This is useful for class-aware factories and inherited constructors.

Follow-up: Why is cls preferable to naming the base class directly? Because calls on derived classes receive the derived class object.

  • staticmethod
  • property
  • Function definitions

Source: Built-in Functions. Checked 2026-10-15.