Python Static Methods Vs Class Method
In this article, we will learn about the difference between a static method and a class method in Python. We will use some built-in functions available in Python and some related custom examples as well. We will compare both methods with examples in this module. Let's first have a quick look over the term decorator, what is a static method, what is a class method, when to use these methods, and compare its working.
What is a Decorator in Python?
Before reading the differences between static and class methods, you must know what is a decorator in Python. Decorators are simple functions. The user can write them, or include them in Python standard library. Decorators are used for performing logical changes in the behavior of other functions. They are an excellent way to reuse code and can they help to separate logic into individual bits. Python provides decorators to define static and class methods.
Static Methods
Static methods are methods that are related to a class but do not need to access any class-specific data. There is no need to instantiate an instance because we can simply call this method. Static methods are great for utility functions. They are totally self-contained and only work with data passed in as arguments.
- This method is bound to the class but not to the object of the class.
- This method can not access or modify class state.
- This method is defined inside a class using
@staticmethod
decorator.
- It does not receive any implicit first argument, neither
self
nor cls
.
- This method returns a static method of the function
Example: Static Methods
class static_example(object):
#decorator
@staticmethod
def fun(arg1, arg2, ...):
...
Class Methods In Python
Class methods are methods that are related to a class and have access to all class-specific data. It uses @classmethod,
a built-in function decorator that gets evaluated after the function is defined. It returns a class method function. It receives the cls
parameter instead of self
as the implicit first argument.
- This method is also bound to the class but not to the object of the class.
- This method can access the state of the class, therefore it can modify the class state that will be applicable to all the instances.
- This method is defined inside a class using
@classmethod
decorator.
- It takes
cls
as a parameter that points to the class and not to the instance of the object.
Example: Define Class Method
class class_example(object):
#decorator
@classmethod
def func(cls, arg1, arg2, ...):
....
Working Example of Static and Class Methods
The below example shows how static and class methods work in a class. Class methods are used for factory purposes, that's why in the below code @classmethod details() is used to create a class object from a birth year instead of age. Static methods are utility functions and work on data provided to them in arguments.
from datetime import date
class Person:
#class constructor
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def details(cls, name, year):
return cls(name, date.today().year - year)
@staticmethod
def check_age(age):
return age > 18
#Driver's code
person1 = Person('Mark', 20)
person2 = Person.details('Rohan', 1992)
print(person1.name, person1.age)
print(person2.name, person2.age)
print(Person.check_age(25))
Mark 20
Rohan 29
True
Difference between a static method and a class method
Static Method
|
Class Method
|
The @staticmethod decorator is used to create a static method.
|
The @classmethod decorator is used to create a class method.
|
No specific parameters are used.
|
It takes cls as the first parameter.
|
It cannot access or modify the class state.
|
It can access or modify the class state.
|
Static methods do not know about the class state. These methods are used to do some utility tasks by taking some parameters.
|
The class method takes the class as a parameter to know about the state of that class.
|
Static methods are used to do some utility tasks.
|
Class methods are used for factory methods.
|
It contains totally self-contained code.
|
It can modify class-specific details.
|
Conclusion
In this article, we learned about decorators in Python, static methods, and class methods. We learned the working of both methods. We saw key differences between the two methods and how a class defines them.