Class method vs Static method in Python (original) (raw)

Last Updated : 26 Jul, 2024

In this article, we will cover the basic **difference between the class method vs Static method in Python and **when to use the class method and static method in python.

**What is Class Method in Python?

The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that evaluation shadows your function definition. A class method receives the class as an implicit first argument, just like an instance method receives the instance

**Syntax Python Class Method:

**class C(object): @classmethod def fun(cls, arg1, arg2, ...): .... **fun: function that needs to be converted into a class method **returns: a class method for function.

**What is the Static Method in Python?

A static method does not receive an implicit first argument. A static method is also a method that is bound to the class and not the object of the class. This method can’t access or modify the class state. It is present in a class because it makes sense for the method to be present in class.

**Syntax Python Static Method:

**class C(object): @staticmethod def fun(arg1, arg2, ...): ... **returns: a static method for function fun.

**Class method vs Static Method

The difference between the Class method and the static method is:

**When to use the class or static method?

**How to define a class method and a static method?

To define a class method in python, we use @classmethod decorator, and to define a static method we use @staticmethod decorator.
Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods. In the below example we use a class method to create a person object from birth year.

As explained above we use static methods to create utility functions. In the below example we use a static method to check if a person is an adult or not.

One simple Example :

class method:

Python `

class MyClass: def init(self, value): self.value = value

def get_value(self):
    return self.value

Create an instance of MyClass

obj = MyClass(10)

Call the get_value method on the instance

print(obj.get_value()) # Output: 10

`

**Static method:-

Python `

class MyClass: def init(self, value): self.value = value

@staticmethod
def get_max_value(x, y):
    return max(x, y)

Create an instance of MyClass

obj = MyClass(10)

print(MyClass.get_max_value(20, 30))

print(obj.get_max_value(20, 30))

`

**Below is the complete Implementation

Python3 `

Python program to demonstrate

use of class method and static method.

from datetime import date

class Person: def init(self, name, age): self.name = name self.age = age

# a class method to create a Person object by birth year.
@classmethod
def fromBirthYear(cls, name, year):
    return cls(name, date.today().year - year)

# a static method to check if a Person is adult or not.
@staticmethod
def isAdult(age):
    return age > 18

person1 = Person('mayank', 21) person2 = Person.fromBirthYear('mayank', 1996)

print(person1.age) print(person2.age)

print the result

print(Person.isAdult(22))

`

**Output:

21 25 True

**Auxiliary Space: O(1)