Introduction
classmethod and staticmethod are two important decorators in Python, the @classmethod decorator helps in declaring a method inside the class and that method can be called using an object of the class or can be called using ClassName.methodName()
The @staticmethod decorator helps in defining a static method inside the Python class, let’s explore each of these decorators in detail.
@classmethod
The purpose of classmethod is used to access and modify the class state, the classmethod in Python should be used when we want to create factory methods inside the class. The factory method is a creational pattern that uses factory methods for creating objects.
The classmethod decorator in Python helps in declaring a method inside the class and the first parameter of the method decorated by @classmethod should always be class. The classmethod can be called using ClassName.methodname or using an object. The classmethod can be invoked by other objects as well.
The below example showcases the conversion of a list of tuples to a list of an employee object, the employee objects are created using the @classmethod object factory.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
@classmethod
def transformToEmpList(cls, employees):
employeeList = []
for emp in employees:
employee = cls(emp[0], int(emp[1]))
employeeList.append(cls(emp[0], int(emp[1])))
return employeeList
Executing the transformation
tupleList = [("Dave", 2000), ("Mike", 4000)]
emplist = Employee.transformToEmpList(tupleList)
for emp in emplist:
print(type(emp))
print(emp.name, emp.salary)
Result
@staticmethod
A static method should be used for the tasks which can be performed independently, the static methods are bound to a class rather than the object of the class that’s why these methods can be called directly using the name of the class.
The static methods don’t have access to the class variables and instance variables and these methods don’t receive the ‘cls’ or ‘self’ arguments.
The static methods should be used for solving utility problems like finding min, max in the list of numbers which can be used for anywhere in the project or solving any mathematical kind of calculations, casting operations, etc. Let's add a staticmethod in the Employee class which confirms whether an employee is senior or junior based on salary.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
@classmethod
def transformToEmpList(cls, employees):
employeeList = []
for emp in employees:
employee = cls(emp[0], int(emp[1]))
employeeList.append(cls(emp[0], int(emp[1])))
return employeeList
@staticmethod
def isSeniorEmployee(salary):
return salary > 3000
tupleList = [("Dave", 2000), ("Mike", 4000)]
emplist = Employee.transformToEmpList(tupleList)
for emp in emplist:
print(type(emp))
print(Employee.isSeniorEmployee(emp.salary))
Results in
Summary
Let’s summarize the characteristics of classmethod and staticmethod,
@classmethod
- Should be used for object creation kind of use cases
- The first parameter of the method should always be ‘cls’
- The classmethod can be called using ClassName.methodName as well as using the object.
- The method should always return the object of the class.
@staticmethod
- Should be used for declaring static methods
- The method cannot receive ‘cls’ or ‘self’ parameters
- For clarity try to always call the static method using the ClassName.method although it can be called using an object as well.