Python

Class and Static Method in Python: Differences

Class and Static Method in Python: Differences

What is Class Method in Python?

The built-in function decorator @classmethod is an expression that is evaluated just after the definition of your function. The result of such a evaluation casts a shadow over your function definition. Similar to how an instance method receives the instance, a class method also takes the class as an implicit first argument.

Syntax of Python Class Method:

Below is the syntax for using the Class Method in python:

class ClassName(object):
    @classmethod
    def method(cls, arg1, arg2, ...):
      ....

Return Value:

It returns a Class method for the function.

Class Method Properties

  • A class method is one that is attached to the class itself rather than the class's object.
  • As it takes a class parameter that points to the class and not the object instance, they have access to the class's state.
  • It has the ability to change a class state that would impact every instance of the class. For instance, it could change a class variable that would affect all instances.

What is Static Method in Python?

A static method is a method that is tied to the class instead of the class's object. It is not possible to pass an implicit first argument to a static method. The class state cannot be accessed or changed by this method. It is present in a class because having the method in a class makes sense.

Syntax of Python Static Method:

Below is the syntax for using the Static Method in python:

class ClassName(object):
    @staticmethod
    def method(arg1, arg2, ...):
      ....

Return Value:

It returns a Static method for the function.

Static Method vs Class Method

The Difference between Class Method and Static method is stated below.

  • While a static method requires no specific parameters, a class method takes cls as its first argument.
  • While a static method cannot access or modify the class state, a class method can.
  • Static methods are typically unaware of the class state. They are utility methods that operate on some parameters after receiving them. On the other hand, class must be a parameter for class methods.
  • Python's @classmethod decorator and @staticmethod decorator are used to create class methods and static methods, respectively.

When do we use Class Methods and Static Methods

  • To create factory methods, we typically use the class method. For various use cases, factory methods return class objects that resemble constructors.
  • To create utility functions, we frequently use static methods.

How to Define and Use Class Methods and Static Methods

For Defining a class method, we use @classmethod decorator and for defining a static method, we use @staticmethod decorator.

For understanding them better, lets take a look at an example in which we are using both the class method and static method.

Example: Using Class methods and static methods

# Python program to demonstrate
# use of class method and static method.
from datetime import date


class Vehicle:
def __init__(self, name, tyre):
self.name = name
self.tyre = tyre

# a class method to create a Car object by name.
@classmethod
def checkBike(cls, name):
    if(name=="Bike"):
    return cls(name, 2)

# a static method to check if the Vehicle is car or not
@staticmethod
def isCar(name):
return name == "Car"


vehicle1 = Vehicle.checkBike('Bike')
vehicle2 = Vehicle('Car', 4)

print(vehicle1.tyre)
print(vehicle2.tyre)

# print the result
print(Vehicle.isCar("Car"))

Output:

2
4
True

write your code here: Coding Playground