Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Python `classmethod()` Function: A Complete Guide With Examples

Python’s @classmethod passes the calling class as cls, making it useful for class-level behavior and alternate constructors that work with subclasses.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s @classmethod decorator makes the class—not an instance—the method’s implicit first argument. That lets a method work with class-level behavior or act as an alternate constructor, and it means an inherited class method receives the subclass when called through that subclass.

What does classmethod() do?

classmethod transforms a function into a class method. The method’s first parameter is conventionally named cls; Python supplies the relevant class automatically. The Python documentation describes the binding as analogous to an instance method receiving its instance, except a class method receives the class. See the Python built-in functions documentation.

As an Amazon Associate I earn from qualifying purchases.

In normal code, use the decorator form:

class User:
    @classmethod
    def guest(cls):
        return cls("guest", is_active=True)

    def __init__(self, name, is_active):
        self.name = name
        self.is_active = is_active

user = User.guest()

When User.guest() is called, cls is User. A class method can also be called through an instance, as in User().guest(); Python supplies the instance’s class, not the instance itself. If the method is inherited and invoked through a derived class, Python supplies that derived class.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How class methods differ from instance and static methods

Method kind Implicit first argument Use it when
Instance method The instance, conventionally self The operation needs or changes per-object state.
Class method The class, conventionally cls The operation needs the calling class or should construct an instance of that class.
Static method None The function belongs conceptually in the class namespace but needs neither an instance nor a class argument.

The distinction is about binding, not simply access to class variables. A class method receives the class used for the call, so inherited methods can respond to subclasses. A static method receives no implicit argument. The Python descriptor guide explains these binding rules, including the class method behavior for calls through a class or an object.

Use @classmethod for alternate constructors

An alternate constructor accepts a different representation of the data, then converts or validates it before creating an object. Use cls(...) when callers should be able to invoke the constructor through a subclass and get an instance of that subclass.

class DateParts:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

    @classmethod
    def from_iso(cls, text):
        year, month, day = map(int, text.split("-"))
        return cls(year, month, day)

DateParts.from_iso("2026-10-05") creates an instance using the parsed components. If a subclass inherits from_iso and calls it through its own class, cls is that subclass; the constructor can therefore create the subclass rather than always creating DateParts. The descriptor guide demonstrates this subclass-preserving pattern with Dict.fromkeys.

Choosing the right method type

  • Choose an instance method if the operation needs a particular object’s attributes or changes that object.
  • Choose a class method if it needs the calling class, class-level behavior, or subclass-aware construction.
  • Choose a static method only if neither the instance nor the class needs to be supplied. If a helper does not use class behavior, a plain function may communicate its purpose more clearly.

Common classmethod mistakes

  • Calling the first parameter self. The conventional name is cls, because the argument represents a class object, not an instance.
  • Hard-coding the base class in an alternate constructor. Returning BaseClass(...) always constructs that named class. Return cls(...) when inherited calls should construct the subclass.
  • Using a class method for every helper. If a function needs neither class nor instance, a static method or a standalone function may be a clearer fit.
  • Calling an instance method through the class without an instance. An instance method still needs an instance as its first argument; a class method instead receives the class automatically.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Python version notes: metadata and descriptor stacking

Python’s built-in documentation records several version-specific changes. Python 3.9 allowed a class method to wrap other descriptors, such as property(). That support was deprecated in Python 3.11 and removed in Python 3.13, so stacking @classmethod with @property is not a supported current pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python 3.10 changed class method metadata behavior: class methods began inheriting attributes such as __module__, __name__, __qualname__, __doc__, and __annotations__, and gained __wrapped__. Consult the version history in the built-in functions documentation and descriptor guide when working with code that targets older Python versions.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.