Python Methods Types

Python Methods Types

In this chapter, we will discuss the Python Methods Types. The different kinds of methods are listed below.

  • Instance Methods 
    • Accessor Methods 
    • Mutator Methods 
  • Class Methods 
  • Static Methods

Instance Methods – Python Methods Types

The methods that perform on the instance variables of a class are called Instance methods. Since they depend on the instances you’d create, you can access the name in the format of instancename.methodname().

class Team:
  #constructor method
  def __init__(self, team_name = '', p = 0):
    self.teamname = team_name
    self.points = p
  
  #instance method
  def outputmethod(self):
    print("Hi {} team".format(self.teamname))
    print("Your points: {}".format(self.points))
  
  #instance method to determine points
  def determinepoints(self):
    if(self.points>=100):
      print("Your team is the best")
    elif(self.points>=75):
      print("Your team is the second best team")
    elif(self.points>=50):
      print("Your team is the third best team")
    elif(self.points>=25):
      print("Your team should get better")

num = int(input("Enter the number of teams: "))
i=0
while(i<num):
  team_name = input("Enter the name of the team: ")
  p = int(input("Enter the number of points scored by team: "))

  t = Team(team_name,p)
  t.outputmethod()
  t.determinepoints()
  i+=1
  print("*****************")
Output

Enter the number of teams: 2
Enter the name of the team: Chennai Super Kings
Enter the number of points scored by team: 100
Hi Chennai Super Kings team
Your points: 100
Your team is the best
*****************
Enter the name of the team: Some other team
Enter the number of points scored by team: 35
Hi Some other team team
Your points: 35
Your team should get better
*****************

In the program mentioned above, besides the constructor, we have two instance methods – outputmethod() and determinepoints() that display the entered values and determine the performance, respectively.

We have two kinds of instance methods in Python: accessor or getter and mutator or setter.

The accessor or getter methods are usually prefixed with ‘get’. For example, getName(). We only read or access data in these methods and do not perform any modification.

On the other hand, the mutator or setter methods modify the values of the variables and are prefixed usually with ‘set’. For example, setName(). Let us now look at a program to understand Python’s accessor and mutator methods.

#Python program to show accessor and mutator methods
class Employee:
  #mutator method
  def setName(self,name):
    self.name = name
  
  #accessor method
  def getName(self):
    return self.name
  
  #mutator method
  def setSalary(self,salary):
    self.salary = salary
  
  #accessor method
  def getSalary(self):
    return self.salary

num = int(input("Enter the number of employees: "))
i=0
while (i<num):
  e = Employee()
  #setting values
  name = input("Enter employee name: ")
  e.setName(name)
  salary = int(input("Enter the salary: "))
  e.setSalary(salary)
  #getting values
  print("Dear {}".format(e.getName()))
  print("Your salary is: {}".format(e.getSalary()))
  i+=1
  print("****************")
Output

Enter the number of employees: 2
Enter employee name: Raju
Enter the salary: 20000
Dear Raju
Your salary is: 20000
****************
Enter employee name: Ramu
Enter the salary: 25000
Dear Ramu
Your salary is: 25000
****************

In the above program, setName() and setSalary() are mutator methods as some modifications or assignments happened there. The methods getName() and getSalary() are accessor methods as they only return the values.

Class Methods – Python Methods Types

Just as it sounds, these are methods that work on a class level by acting on the static or class variables. We use a decorator called @classmethod above each class method. The first parameter in each class method is ‘cls‘ by default.

#Python program to illustrate Class Methods
class Fish:
  #class variable
  eyes = 2

  #class method
  @classmethod
  def swim(cls,fname):
    print("{} swims with {} eyes.".format(fname,cls.eyes))

Fish.swim("Sardine")
Fish.swim("Mackerel")
Output

Sardine swims with 2 eyes.
Mackerel swims with 2 eyes.

The swim() method is a class method in the program above, and its first parameter is ‘cls’.

Static Methods – Python Methods Types

The Static Methods are also processed at the class level, but it does not affect the instances. It is used chiefly for operations that do not involve instances such as updating attributes in another class, setting environmental variables etc. We use the decorator @staticmethod above the static methods.

#Python program to show the static methods
class StaticExample:
  n=0 #static or class variable
  def __init__(self):#constructor method
    StaticExample.n = StaticExample.n+1
  
  #static method to calculate the number of instances created so far
  @staticmethod
  def noOfInstances():
    print("No. of instances created so far: ",StaticExample.n)

ins1 = StaticExample()
ins2 = StaticExample()
ins3 = StaticExample()
StaticExample.noOfInstances()
Output

No. of instances created so far:  3

In the above program, the static method, noOfInstances() prints the number of instances created for the StaticExample class.

Scroll to Top