Class Method in Python for Data Science:
The classmethod is denoted by @classmethod. It is a built-in function decorator. It gets evaluated after the function is defined. The result of the @classmethod function shadows our function definition. The method receives the class as implicit first argument similar to an instance method receiving the instance.
- Class method is bound to the class and not to the class object.
- It has access to the state of the class.
- It is capable of modifying the state of the class. This modification will then be applicable to all the instances of the class.
It does not receive an implicit first argument that classic method received.
- Similar to the class method, it is also bound to the class and not the class object.
- It is unable to access or modify the class state.
- The method is present in a class.
Difference between Class Method and Static Method:
- A class method takes class as the first parameter, but in static method there is no need for specific parameters.
- A class method is able to access or modify the class state, but this is not possible in the case of static method.
- @classmethod decorator is there to create a class method, but @staticmethod decorator is there in order to create a static method.
Usage of static and class methods:
- In general class method is there for creating factory methods.
- In general static method is there for creating utility functions.