Types of Methods: ================= Four types of methods: 1) Instance Methods 2) Getters and Setter methods 3) Class Methods 4) Static methods Instance Methods: ----------------- => A method within the class can be defined for the creation/definition of instance variables called as "Instance Method. => Instance variables can be defined within the constructor or instance method using "self" keyword. => An instance method must be defined with at least one parameter that is "self". for the self no value need to assign. => The instance method can be accessed within the class using "self" and in outside the class, we can access/call using the object. Syntax: def instance-method-name(self): any behavior def instance-method-name(self, par1, par2,...): any behavior Calling of instance method: --------------------------- within the class: ------------------ Syntax: class ClassName: def __init__(self, p1, p2, ....): self.par1 = p1 self.par2 = p2 def m1(self): behavior def m2(self): self.m1() In outside the class: ---------------------- cn = ClassName(v1, v2,...) cn.m2() cn.m1() Note: ----- In the class of python, the constructor is not mandatory. If no constructor has defined within the class means, the PVM can create the default constructor. 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("You 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 the student name:") marks = int(input("Enter the student marks:")) s = Student(name,marks) s.display() s.grade() print() 2) Setter and Getter Methods: ------------------------------ Setter method: -------------- => this method can be used to set values to the instance variables. => setter method is also called as "mutator method". Syntax: def setVariable(self, variable): self.variable = variable Ex: ---- def setName(self, name): self.name = name def setAge(self, age): self.age = age Getter method: -------------- => this method can be used to get the values of the instance variables. => Getter method is also called as "accessor method". Syntax: def getVariable(self): return self.Variable Ex: def getName(self): return self.name def getAge(self): return self.age 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 the student name:") s.setName(name) marks = int(input("Enter student marks:")) s.setMarks(marks) print("Hi",s.getName()) print("Your marks are:",s.getMarks()) print() 3) Class methods: ----------------- => A method with class variables or static variables called as "Class method". => class variable always define with "cls" => static variable always define with "class name". => The class method must be define with at least one parameter called as "cls". => Above the class method definition, we must be add a decorator called as "@classmethod" Ex: class ClassName: @classmethod def method(cls): cls.var = value ClassName.var1 = value => The class method must be access/call with the class name Syntax: ClassName.classMethod() => The class method can also accept or call using the class object. Syntax: obj-name.classMethod() class Animal: legs = 4 # class variable @classmethod def walk(cls,name): print("{} walks with {} legs..".format(name,cls.legs)) Animal.walk('Dog') Animal.walk('Horse') anm = Animal() anm.walk("Cat") 4. Static Methods: ------------------- => also called as "utility method". => Within the static method, we are not allowed to define the instance variables and class variables. Nots: ----- static methods always with static variables only. => the static method must be define with a decorator named as "@staticmethod" Syntax: @staticmethod def methodName(p1, p2, p3,..): behavior Note: ----- The static method never accept any word like: "self" or "cls" Calling of static method is: ClassName.staticMethodName() class MyClass: @staticmethod def add(x,y): print("The sum = ",x+y) @staticmethod def mul(x,y): print("The product = ",x*y) mc = MyClass() mc.add(10,20) mc.mul(100,34) # static method never consider to call with object of the class. MyClass.add(100,300) MyClass.mul(123,43)