Class Methods¶
What is a Class Method?¶
A class method is a method that operates on the class rather than a specific instance.
Unlike an instance method, Python automatically passes the class as the first argument, conventionally named cls.
Use a class method when the logic needs to work with class-level data or create new instances of the class.
Creating a Class Method¶
Python provides the @classmethod decorator.
class User:
company = "Google"
@classmethod
def show_company(cls):
print(cls.company)
User.show_company()
Output
Python automatically passes the class as cls.
Conceptually,
becomes
Understanding cls¶
cls is a reference to the class itself.
Just as self refers to an object,
cls refers to the class.
Memory Representation¶
Calling
binds the class.
Inside the method,
Why Do We Need Class Methods?¶
Suppose we want to create users from a string.
Without a class method,
This parsing logic may be needed in multiple places.
Instead,
we can move it into the class.
Factory Method¶
class User:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_string(cls, data):
name, age = data.split(",")
return cls(name, int(age))
Usage
The class method creates and returns a new object.
This is called a factory method or alternative constructor.
Why Use cls Instead of User?¶
Notice we returned
instead of
This supports inheritance.
Suppose
Now,
returns
not
Because
Using cls makes the method reusable across subclasses.
Instance Method vs Static Method vs Class Method¶
Instance Method¶
Static Method¶
Class Method¶
When Should You Use a Class Method?¶
Use a class method when:
- Creating objects in different ways.
- Accessing or modifying class variables.
- Implementing factory methods.
- Writing behavior that belongs to the class rather than an individual object.
Example¶
class Employee:
company = "Google"
@classmethod
def change_company(cls, name):
cls.company = name
Employee.change_company("OpenAI")
print(Employee.company)
Output
The class variable changes for every instance.
Real-World Examples¶
from_json()¶
from_dict()¶
datetime.strptime()¶
Creates a datetime object from a formatted string.
pathlib.Path.cwd()¶
Returns an instance of Path.
Many standard library APIs use class methods as alternative constructors.
Common Interview Questions¶
What is a class method?¶
A class method operates on the class instead of an instance. Python automatically passes the class as the first argument (cls).
What is cls?¶
cls is a reference to the class itself. It is automatically passed whenever a class method is called.
Why do we use @classmethod?¶
@classmethod is used when the method needs to access class-level data or create instances of the class without relying on an existing object.
What is a factory method?¶
A factory method is a class method that creates and returns objects. It provides an alternative way to construct instances besides calling the constructor directly.
Why use cls instead of the class name?¶
Using cls supports inheritance. If the method is called from a subclass, it automatically creates an instance of that subclass instead of the parent class.
Can a class method access instance variables?¶
No.
A class method doesn't receive an instance, so it cannot directly access instance attributes.
Can a class method access class variables?¶
Yes.
This is one of its primary use cases.
Difference between instance, static, and class methods?¶
| Method | First Argument | Access Instance Data | Access Class Data |
|---|---|---|---|
| Instance Method | self |
✅ | ✅ |
| Static Method | None | ❌ | Only explicitly |
| Class Method | cls |
❌ | ✅ |
Common Interview Mistakes¶
❌ Using a class method when instance data is required.
✔ Use an instance method whenever object state is involved.
❌ Returning User(...) inside a class method.
✔ Return cls(...) to support inheritance.
❌ Thinking class methods belong to objects.
✔ Class methods operate on the class itself.
Interview Tip¶
If asked,
When would you use a class method?
A strong answer is:
"I primarily use class methods to implement factory methods and alternative constructors. They're also useful when the logic operates on class-level data rather than a specific instance. Using
clsinstead of the class name makes the implementation inheritance-friendly."
Key Takeaways¶
- Class methods operate on the class, not an instance.
- Python automatically passes the class as
cls. - Use
@classmethodfor factory methods and class-level operations. - Prefer
cls(...)over the class name to support inheritance. - Class methods can access and modify class variables.
- They are commonly used as alternative constructors.
Python Methods
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
Instance Method Static Method Class Method
self None cls
│ │ │
Object Utility Class
│ │ │
Reads/Writes Helper Logic Factory Methods Instance State Class State
Example: Example: Example: user.rename() Math.add() User.from_json()