Difference between static method and class method in Python

In this tutorial, we will learn the class method and static method in Python and also the difference between a static method and class method.

Class Method

  • It is a method which is bound to the class and not the object of the class.
  • The class state can be accessed and modified by the class method.
  • @classmethod decorator is used in a class method.
    Syntax:

    class my_class:
       @classmethod
      def class_method(cls, arguments):
          #Body of function
          return value

Static Method

  • It is a method which is bound to the class and not the object of the class.
  • The class state can’t be accessed or modified by the static method.
  • @staticmethod decorator is used in a static method.
    Syntax:

    class my_class:
       @staticmethod
       def static_method(arguments):
          #Body of function
          return value

     

Difference between a class method and a static method

  • The class method takes cls (class) as the first argument and in case of static method, it does not take any specific parameter.
  • The class state can be accessed and modified by the class method but it can’t be accessed or modified by the static method.
  •  For the class method  @classmethod decorator is used and for a static method @staticmethod decorator is used.

Example Code

Below is given our Python code example:

from datetime import date 
class hospital:
   def __init__(self, name, age):
      self.name = name
      self.age = age
   @staticmethod
   def isAdult(age):
      if age > 18:
         return True
      else:
         return False
   @classmethod
   def patient_from_year(patient_class, name, year):
      return patient_class(name, date.today().year - year)
   def __str__(self):
      return 'patient Name: {} and Age: {}'.format(self.name, self.age)
p1 = hospital('Aditya', 25)
print(p1)
p2 = hospital.patient_from_year('Ganesh', 1987)
print(p2)
print(hospital.isAdult(25))
print(hospital.isAdult(16))

To define a class method in Python, we used @classmethod decorator and to define a static method we used @staticmethod decorator. Now lets us look at an above code to understand the difference between a class method and a static method. Just think about that, we want to create a class with the name Hospital.

As the Python language doesn’t support method overloading like some of those programming languages, for example, C++ or Java so here we use class methods to create factory methods and static methods to create utility functions. In above code class method is used to create a Hospital object from the birth year and the static method is used to check if a person is an adult or not.

 

OUTPUT:

The output of our program will be looks like you can see below:

patient Name: Aditya and Age: 25
patient Name: Ganesh and Age: 32
True
False

The birth year of Ganesh is given as input and output is Ganesh’s present age. We calculated Ganesh’s age using the function which is in a class method. The function present in a static method check the patient is adult or not.

Leave a Reply

Your email address will not be published. Required fields are marked *