Class and Static Methods in Python
The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods. So methods are a piece of code that can be reused in any program we do. There are mainly three different types of methods they are:
- Instance method
- Class method
- Static method
In this article, we will discuss the class method and static methods in python. Lets us also learn about the differences between them and the usage of these methods.
The class method in python:
The @class method decorator is a built-in function. Once the function is defined, this method gets evaluated and covers the full function after evaluation. A class method will receive the class as an implicit argument. And the syntax for this method is given as follows
class c(object):
@classmethod
Def fun(cls, arg1, arg2, arg3, ….):
Here the fun is a function that needs to get converted into a class.
- A Class method is one of the method that is completely used to the class but not the object of the class.
- They have access to the state of the class because it takes a class parameter that refers to the class and the object instance.
- A class state could be modified, which would apply across all other class instances.
Static method in python:
In the static method, we don't get any implicit first argument. The static method is also needed to the class but not with the object of that class. With this method, we can't access or modify any class state. The static method is present in a class because it makes a meaning for the method to be present in a class. The syntax used for this method is given as follows
class c(object):
@staticmethod
def fun(arg1, arg2, arg3, …..):
with this, we will get a static method for the fun function.
How do we define a class method and a static method:
As discussed above, we use the @classmethod decorator and @staticmethod decorator to implement class and static methods. Let us have a look and understand this concept by taking an example code where we use a static method to check whether the person is an adult or a child. As python doesn't support method overloading, unlike other programming languages, we take the class methods to create a person's object from his birth year. Let us look at the example code:
Code:
# program to understand the use of the class method and static method.
from datetime import date
class Person:
def __init__(self, pname, age):
self.pname = pname
self.age = age
# using a class method to create a Person by taking their birth year.
@classmethod
def from_birthyear(cls, pname, year):
return cls(pname, date.today().year - year)
# using a static method to check wether a Person is an adult or not.
@staticmethod
def isAdult(age):
return age > 18
personx = Person('varun', 19)
persony = Person.from_birthyear('srinivas', 1974)
print(personx.age)
print(persony.age)
# print the result
print(Person.isAdult(25))
Output:
19
48
True
Explanation:
So in this program, we first imported the required packages and then created a class and gave its attributes. Next, we used a class method to create a person by using the object's birth year. Then we used the static method to check wether a person was an adult or not. Then we were given the input and output; after execution, we got this output.
So we generally use the class methods while creating factory methods, and the factory method will return the class object for different cases. And static methods are used when they create utility functions.
The difference between the static and class methods are:
- A class method needs parameters while taking a class, but the static method doesn't require any parameters.
- A class method can modify any class state, whereas a static method has no access to modify the class state.
- Static Methods are not involves in the class state. But they only work on utility-type methods.
- To create class methods in python, we use @classmethod decorator, whereas to create a static method in python, we use @staticmethod decorator.
Conclusion:
So far in this article, we have learned about python methods where static and class methods are thoroughly discussed using example codes and basic syntaxes.