Ad Code



Python: Types of Methods

Inside Python class 3 types of methods are allowed

  1. Instance Methods
  2. Class Methods
  3. Static Methods

 1. Instance Methods:

Inside method implementation, if we are using instance variables then such types of methods are called instance methods. 

Inside instance method declaration, we have to pass the self variable.

 def m1(self):

By using self variable inside method we can able to access instance variables.

Within the class we can call instance method by using self variable and from outside of the class 

we can call by using object reference.

1) class Student: 
2) def __init__(self,name,marks): 
4) self.marks=marks 
5) def display(self): 
6) print('Hi',
7) print('Your Marks are:',self.marks) 
8) def grade(self): 
9) if self.marks>=60: 
10) print('You got First Grade') 
11) elif self.marks>=50: 
12) print('Yout got Second Grade') 
13) elif self.marks>=35: 
14) print('You got Third Grade') 
15) else: 
16) print('You are Failed') 
17) n=int(input('Enter number of students:')) 
18) for i in range(n): 
19) name=input('Enter Name:') 
20) marks=int(input('Enter Marks:')) 
21) s= Student(name,marks) 
22) s.display() 
23) s.grade() 
24) print() 


Enter number of students:2
Enter Name:Durga
Enter Marks:90
Hi Durga
Your Marks are: 90
You got First Grade
Enter Name:Ravi
Enter Marks:12
Hi Ravi
Your Marks are: 12
You are Failed

Setter and Getter Methods:

We can set and get the values of instance variables by using getter and setter methods.

Setter Method:

setter methods can be used to set values to the instance variables. setter methods also known as mutator methods.


def setVariable(self,variable):


def setName(self,name):

Getter Method:

Getter methods can be used to get values of the instance variables. Getter methods also known as accessor methods.


def getVariable(self):
 return self.variable


def getName(self):

2. Class Methods:

Inside method implementation if we are using only class variables (static variables), then such type of methods we should declare as class method.

We can declare class method explicitly by using @classmethod decorator. 

For class method we should provide cls variable at the time of declaration

We can call classmethod by using classname or object reference variable.

Demo Program:

1) class Animal: 
2) legs=4 
3) @classmethod 
4) def walk(cls,name): 
5) print('{} walks with {} legs...'.format(name,cls.legs)) 
6) Animal.walk('Dog') 
7) Animal.walk('Cat') 
Dog walks with 4 legs...
Cat walks with 4 legs...

3. Static Methods:

In general these methods are general utility methods.

Inside these methods we won't use any instance or class variables.

Here we won't provide self or cls arguments at the time of declaration.

We can declare static method explicitly by using @staticmethod decorator

We can access static methods by using classname or object reference

1) class DurgaMath: 
3) @staticmethod 
4) def add(x,y): 
5) print('The Sum:',x+y) 
7) @staticmethod 
8) def product(x,y): 
9) print('The Product:',x*y) 
11) @staticmethod 
12) def average(x,y): 
13) print('The average:',(x+y)/2) 
15) DurgaMath.add(10,20) 
16) DurgaMath.product(10,20) 
17) DurgaMath.average(10,20) 
The Sum: 30
The Product: 200
The average: 15.0

"Python Types Of Methods"

"Types Of Class Methods In Python"

"Different Types Of Class Methods In Python"

"Types Of String Methods In Python"

"How Many Types Of Methods In Python"

"Different Types Of List Methods In Python"

"Python Methods Within Methods"

"What Are The Methods In Python"

"Python Class Types Of Methods"

"Python Different Types Of Methods"