@classmethod
A method that receives the class (cls) instead of an object. It is most often used as an alternative constructor.
Example
#class Student:
def __init__(self, name, points):
self.name = name
self.points = points
@classmethod
def from_text(cls, text):
name, points = text.split(";")
return cls(name, int(points))
@classmethod
def from_dict(cls, data):
return cls(data["name"], data.get("points", 0))
emma = Student.from_text("Emma;250")
jack = Student.from_dict({"name": "Jack"})
print(emma.name, emma.points)
print(jack.name, jack.points)Definition and usage
#The @classmethod decorator creates a class method. Its first argument is not an object but the class itself, conventionally named cls. You usually call it through the class name, e.g. Student.from_text("Emma;250").
Its most common use is as an alternative constructor: a method that builds an object from data in a different format, such as a string, a dictionary, or a line from a file. The method prepares the arguments and finally calls cls(...). This is how the built-in dict.fromkeys() or datetime.fromisoformat() work.
Because cls points to the class the method was called on, in a derived class cls(...) creates an object of the derived class, not the base one. That is an advantage over a static method with a hardcoded class name. Class methods also have convenient access to class attributes, such as shared counters or settings.
Syntax
#class Name:
@classmethod
def method(cls, args):
return cls(...)
Name.method(args)More examples
#class Task:
def __init__(self, number):
self.number = number
@classmethod
def from_code(cls, code):
return cls(code.removeprefix("task_"))
class Quiz(Task):
pass
task = Quiz.from_code("task_7_3")
print(type(task).__name__, task.number)class Player:
online = 0
def __init__(self, nick):
self.nick = nick
Player.online += 1
@classmethod
def status(cls):
return f"Players online: {cls.online}"
Player("emma_dev")
Player("jack99")
print(Player.status())Good practices
#- Start alternative constructor names with
from_, e.g.from_json()orfrom_row(). It is a convention familiar from the standard library. - In a class method, create objects through
cls(...), not through the class name. That way it also works correctly in derived classes. - If a method needs neither the object nor the class,
@staticmethodis enough.
Related entries
#- @staticmethod vs @classmethod@staticmethod behaves like a plain function with no automatic argument, while @classmethod receives the class as cls and usually serves as an alternative constructor.
- @staticmethodA method that gets neither the object nor the class automatically. It behaves like a plain function placed inside a class.
- __init__()A method called automatically when an object is created. It stores the object’s initial data, its attributes.
- selfThe first parameter of a method: the object the method was called on. It gives access to that object’s attributes and other methods.
- InheritanceBuilding one class on top of another: the derived class inherits the base class’s methods and can extend or override them.
- DecoratorsFunctions that wrap other functions and add behavior to them, such as logging or permission checks. Written as @name.
Spotted a mistake or missing an example? Write to us.