Python class method vs static method

Once you know the difference between class method and static method then you can deal with both of them easily-

Class Method Static Method
Class method is created using @classmethod decorator. Static method is created using @staticmethod decorator.
Class method can only access class variables. Static method cannot access instance variables but can access class variables.
Class method can access other class methods and static methods. Static method can only access other static methods.
Class method can call other class and static methods. Static method cannot call instance methods.
cls is passed as the first argument to class method. Static method has no specific parameters like this.
Class method is used in factory method design pattern. Static method is used as a utility functions.
Class method is also created using classmethod() function. Static method is created using staticmethod() function.

Python Program to show the usage of static method

class Math:

  @staticmethod
  def add(p1, p2):
    return p1+p2

print('Sum of 23 and 54 is', Math.add(23, 54))

Output of the above program

Sum of 23 and 54 is 77

Python Program to show the usage of class method

class Shape:

  @classmethod
  def getInstance(cls, shapeType):
    if(shapeType == 'Circle'):
      return Circle()
    elif(shapeType == 'Square'):
      return Square()

class Circle(Shape):
  def figure(self):
    print('Circular shape.')

class Square(Shape):
  def figure(self):
    print('Square shape.')

c = Shape.getInstance('Circle')
c.figure()
s = Shape.getInstance('Square')
s.figure()

Output of the above program

Circular shape.
Square shape.