Module 11 · Object-Oriented Programming — topic 12 of 25
Class methods
Methods that receive the class (cls) instead of an instance, defined with @classmethod: alternative constructors, factories that respect subclasses, and managing class-level state.
Learning objectives
- Define class methods with @classmethod
- Write alternative constructors that return cls(...)
- Use class methods to work with class-level data
Definition
A class method is defined with the @classmethod decorator and receives the class itself as its first argument, conventionally named cls. It can be called on the class or on an instance.
Introduction
datetime.date.today() and dict.fromkeys() are class methods: you call them on the class to get a new object, without having one first.
Why it matters
Class methods let a class offer several convenient ways to create objects (from a string, a file, a dictionary) while keeping one real constructor.
Detailed explanation
Decorate the method with @classmethod; its first parameter cls receives the class.
Call it as ClassName.method(...) (most common) or obj.method(...) — cls is still the class.
Alternative constructors parse or convert input and end with return cls(...). Using cls instead of the class name means subclasses get instances of the subclass.
Class methods can also read and update class variables (cls.count += 1).
Prefer a class method over a module function when the operation is clearly about creating or configuring this particular type.
Because a class method doesn't receive an instance, it cannot read instance attributes such as self.name. If a method needs data from a particular object, it should be an ordinary instance method instead.
Syntax
- @classmethod receives cls.
- Return cls(...) for factories.
- Works correctly with subclasses.
Code examples
An alternative constructor
2026-11-27
Working with class state
Session: 2 active
More examples
Factories that respect subclasses
Rectangle(3.0, 4.0) 12.0 Triangle(3.0, 4.0, 5.0) 6.0
Step-by-step explanation
- from_csv is written once, in Shape.
- Because it calls cls(...), Rectangle.from_csv builds a Rectangle and Triangle.from_csv builds a Triangle.
- Each subclass then provides its own area method.
Expected output
Running “Factories that respect subclasses” prints exactly:
Practical applications
- datetime.fromisoformat, int.from_bytes, dict.fromkeys.
- Loading configuration objects: Config.from_file(path).
Best practices
- Name alternative constructors from_... or parse.
- Return cls(...) so subclasses work.
- Keep validation in __init__ and have factories call it.
Common mistakes
Hard-coding the class name in a factory
Avoid
Prefer
Using the base class name returns a Shape even when called on a subclass.
Forgetting the decorator
Avoid
Prefer
Without @classmethod, Date.from_string('...') passes the string as cls and fails.
Important notes
- Class methods are inherited like other methods; cls is whichever class they were called on.
Key takeaways
- @classmethod methods receive the class as cls.
- They are ideal for alternative constructors.
- Return cls(...) to support subclasses.
Try it yourself
- 1Add Point.from_tuple and Point.origin class methods.
- 2Write Employee.from_dict(data) that builds an employee from a dictionary.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?