Inside Python class 3 types of methods are allowed
- Instance Methods
- Class Methods
- 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): 3) self.name=name 4) self.marks=marks 5) def display(self): 6) print('Hi',self.name) 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()
output:
D:\durga_classes>py test.py 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.
syntax:
def setVariable(self,variable): self.variable=variable
Example:
def setName(self,name): self.name=name
Getter Method:
Getter methods can be used to get values of the instance variables. Getter methods also known as accessor methods.
syntax:
def getVariable(self): return self.variable
Example:
def getName(self): return self.name
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') Output D:\python_classes>py test.py 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: 2) 3) @staticmethod 4) def add(x,y): 5) print('The Sum:',x+y) 6) 7) @staticmethod 8) def product(x,y): 9) print('The Product:',x*y) 10) 11) @staticmethod 12) def average(x,y): 13) print('The average:',(x+y)/2) 14) 15) DurgaMath.add(10,20) 16) DurgaMath.product(10,20) 17) DurgaMath.average(10,20) Output 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"