Tag: static methods

Difference between Static, abstract and Class Methods in Python

Class MethodStatic Method
requires “cls” as first parameter does not require parameters
has the ability to access the state of objectdoes not influence the state of the object
method decorated with @classmethodmethod decorated with @staticmethod
they can be used to create objectscan be only used as a helper method
More about @classmethodMore about @staticmethod
Abstract Method
requires to import abc package and abc.ABC as first parameter
cannot be instanted, gives TypeError is tried to instantiate Abstract Class
method decorated with @abstractmethod
can be used as super class and implementation should be in subclass
More about @abstractmethod

Python- Static Methods

Static Methods can be used when Class object is not required to call a method but the method’s within that class are related to execute the code.

Also static methods can be used when the state of the object is not required to maintain. i.e. they don’t have the ability to change the state of the object

Decorate class method with @staticmethod keyword to make the method static.

See below example of using Static Method-

validate_instance_counter(param) method is marked as static method.