Method in Python
- Method is nothing but a function defined inside a class.
Syntax :
keyword_class class_name :
keyword_def method_name() :
statement 1
statement 2
Example :
#program to define method
class Employee_data :
def assign_employee_data(self):
self.name = "Techno Xpresss"
def display_employee_data(self,):
print("name :", self.name)
- With the help of Object (reference to class), method can be called
Syntax :
object_name = class_name()
object_name . method_name()
Example :
#program
class Employee_data : # class
def assign_employee_data(self): # method
self.name = "Techno Xpresss"
def display_employee_data(self,): # method
print("name :", self.name)
emp_1 = Employee_data() # object creation
emp_1.assign_employee_data() # method calling with object
emp_1.display_employee_data()
#Outputname : Techno Xpresss
- It is a collection of statement(s) to perform operations.
#program
class Employee_data :
def operations(self, a, b):
print("sum : ", a + b)
print("sub : ", a - b)
print("mul : ", a * b)
print("div : ", a / b)
def assign_employee_data(self):
self.name = "Techno Xpresss"
def display_employee_data(self,):
print("name :", self.name)
#Output
sum : 13
sub : 7
mul : 30
div : 3.3333333333333335
- Once defined can be called many times.
#program
class Employee_data :
def operations(self, a, b):
print("sum : ", a + b)
emp_1 = Employee_data()
emp_1.operations(10,5)
emp_1.operations(1,50)
emp_1.operations(15,25)
emp_1.operations(16,35)
#Output
sum : 15
sum : 51
sum : 40
sum : 51
- Method can be defined in 4 ways
- without parameter without return
#program
class Employee_data :
def operations(self):
print("this is without parameter without return method")
emp_1 = Employee_data()
emp_1.operations()
#Output
this is without parameter without return method
- with parameter without return
#program
class Employee_data :
def operations(self, a, b):
sum = a+b
print("this is with parameter without return method")
emp_1 = Employee_data()
emp_1.operations(10, 20)
#Output
this is with parameter without return method
- without parameter with return
#program
class Employee_data :
def operations(self):
sum = 10 + 20
print("this is without parameter with return method")
return sum
emp_1 = Employee_data()
result = emp_1.operations()
print("return from method : ", result)
#Output
this is without parameter with return method
return from method : 30
- with parameter with return
#program
class Employee_data :
def operations(self, a, b):
sum = a + b
print("this is with parameter with return method")
return sum
emp_1 = Employee_data()
result = emp_1.operations(10,15)
print("return from method : ", result)
#Output
this is with parameter with return method
return from method : 25
- Methods are of 3 types :
- Static Method
#program to define static method
class Employee_data :
@staticmethod
def operations():
print("this is static method")
method defined under decorator @staticmethod
- Class method
#program to define class method
class Employee_data :
@classmethod
def operations(cls):
print("this is class method")
method defined under decorator @classmethod
- Instance method
#program to define instance class
class Employee_data :
def operations(self):
print("this is instance method")
method defined without any decorator
Comments
Post a Comment