Instance method, Class method, and Static method

Dineshbaburam
2 min readMar 16, 2022

Instance method:

  • Instance methods were called by an object which we created for the class.
  • The instance object variable can be a modifier.
  • Use “self” as the first parameter in the instance method.
  • The “self” parameter refers to the current object.
class Student:
# initialization method
def __init__(self, first, last):
#instance variable
self.first = first
self.last = last

# instance class
def get_detail(self, mark):
return self.first + " " + self.last + " " + str(mark)

stud = Student("Ram", "Kumar")

print(stud.get_detail(70))

The output

Ram Kumar 70

Class method:

  • Class method can be accessed only by the class itself, not to any specific instance object.
  • “cls” is the first parameter in the class method when define.
  • @classmethod function to define the class method.
  • Class method can access or modify the class state.
  • Class methods are bound to the class so we should access them using the class name.
class Student:
#class variable
percent = 10
# initialization method
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age

# instance method
def mark(self, mark, percent):
self.mark = mark
self.percent = percent

# class method
@classmethod
def change_per(cls, percent):
cls.percent = percent + percent

stud = Student("Ram", "Kumar", 50)
#calling class variable
Student.change_per(50)
stud.mark(100, Student.percent)
print(stud.__dict__)

The output:

{‘first’: ‘Ram’, ‘last’: ‘Kumar’, ‘age’: 50, ‘mark’: 100, ‘percent’: 100}

Static method:

  • Static method is a general utility method that performs a task in isolation.
  • Static methods have limited use because they don’t have access to the attribute of an object and class attribute.
  • @staticmethod to define a static method.
  • Static method don’t take an instance or class parameter
  • It can’t modify the class or object state.
class Student:
#class variable
percent = 10
# initialization method
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age

# instance method
def mark(self, mark, percent):
self.mark = mark
self.percent = percent

# Static method
@staticmethod
def get_age(age):
return age

stud = Student("Ram", "Kumar",Student.get_age(50))
#calling class variable
stud.mark(100, Student.percent)
print(stud.__dict__)

The output

{'first': 'Ram', 'last': 'Kumar', 'age': 50, 'mark': 100, 'percent': 10}

Example 1:

class Student:
percent = 10
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age

def mark(self, mark, percent):
self.mark = mark
self.percent = percent

@classmethod
def change_per(cls, percent):
cls.percent = percent + percent

@staticmethod
def get_age(age):
return age


stud = Student("Hello", "World", Student.get_age(50))
Student.change_per(20)
stud.mark(100, Student.percent)
print(stud.__dict__)

The output:

{'first': 'Hello', 'last': 'World', 'age': 50, 'mark': 100, 'percent': 40}

--

--