- Inside python class 3 types of methods are allowed
- Instance methods
- Class methods
- Static methods
Instance Methods
- Inside method implementation if we are using instance variables then such type of methods are called instance methods.
- Inside instance method declaration we have to pass 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.
Example
Python
class Student:
def __init__(self,name,marks):
self.name=name
self.marks=marks
def display(self):
print('Hi',self.name)
print('Your Marks are:',self.marks)
def grade(self):
if self.marks>=60:
print('You got First Grade')
elif self.marks>=50:
print('Yout got Second Grade')
elif self.marks>=35:
print('You got Third Grade')
else:
print('You are Failed')
n=int(input('Enter number of students:'))
for i in range(n):
name=input('Enter Name:')
marks=int(input('Enter Marks:'))
s= Student(name,marks)
s.display()
s.grade()
print()
Output
PowerShell
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
Python
def setVariable(self,variable):
self.variable=variable
Example
Python
def setName(self,name):
self.name=name
Output
PowerShell
7
Getter Method
- Getter methods can be used to get values of the instance variables. Getter methods also known as accessor methods.
syntax
Python
def getVariable(self):
return self.variable
Example
Python
def getName(self):
return self.name
Output
PowerShell
6
Example
Python
class Student:
def setName(self,name):
self.name=name
def getName(self):
return self.name
def setMarks(self,marks):
self.marks=marks
def getMarks(self):
return self.marks
n=int(input('Enter number of students:'))
for i in range(n):
s=Student()
name=input('Enter Name:')
s.setName(name)
marks=int(input('Enter Marks:'))
s.setMarks(marks)
print('Hi',s.getName())
print('Your Marks are:',s.getMarks())
print()
Output
PowerShell
D:\python_classes>py test.py
Enter number of students:2
Enter Name:Durga
Enter Marks:100
Hi Durga
Your Marks are: 100
Enter Name:Ravi
Enter Marks:80
Hi Ravi
Your Marks are: 80
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.
Example
Python
class Animal:
legs=4
@classmethod
def walk(cls,name):
print('{} walks with {} legs...'.format(name,cls.legs))
Animal.walk('Dog')
Animal.walk('Cat')
Output
PowerShell
D:\python_classes>py test.py
Dog walks with 4 legs...
Cat walks with 4 legs...
Program to track the number of objects created for a class
Example
Python
class Test:
count=0
def __init__(self):
Test.count =Test.count+1
@classmethod
def noOfObjects(cls):
print('The number of objects created for test class:',cls.count)
t1=Test()
t2=Test()
Test.noOfObjects()
t3=Test()
t4=Test()
t5=Test()
Test.noOfObjects()
Output
PowerShell
7
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.
Example
Python
class DurgaMath:
@staticmethod
def add(x,y):
print('The Sum:',x+y)
@staticmethod
def product(x,y):
print('The Product:',x*y)
@staticmethod
def average(x,y):
print('The average:',(x+y)/2)
DurgaMath.add(10,20)
DurgaMath.product(10,20)
DurgaMath.average(10,20)
Output
PowerShell
The Sum: 30
The Product: 200
The average: 15.0
Note
- In general we can use only instance and static methods.Inside static method we can access
- class level variables by using class name.
- class methods are most rarely used methods in python.
Passing members of one class to another class
- We can access members of one class inside another class.
Example
Python
class Employee:
def __init__(self,eno,ename,esal):
self.eno=eno
self.ename=ename
self.esal=esal
def display(self):
print('Employee Number:',self.eno)
print('Employee Name:',self.ename)
print('Employee Salary:',self.esal)
class Test:
def modify(emp):
emp.esal=emp.esal+10000
emp.display()
e=Employee(100,'Durga',10000)
Test.modify(e)
Output
PowerShell
D:\python_classes>py test.py
Employee Number: 100
Employee Name: Durga
Employee Salary: 20000
In the above application, Employee class members are available to Test class.
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!