Dremendo Tag Line

Instance Methods in Python Programming

OOPs in Python

In this lesson, we will understand what is Instance Method in Python Programming and how to create them along with some examples.

What are Instance Methods in Python?

Instance methods are used to process data stored in instance variables and are used only by the object of the class.

Like the constructor method, the first parameter of the instance method is always self, which refers to the memory address of the instance (object). Using that, we can access and modify the value of instance variables.

Remember that self is not a keyword, so we can use any name in place of self, and using that name, we can access the object's instance variables.

video-poster

Example

class Record:

    # Creating instance variables using constructor method
    def __init__(self):
        self.rollno = 0
        self.name = ""
        self.age = 0

    # Creating an instance method to store data in instance variables
    def inputdata(self):
        self.rollno = int(input('Roll No: '))
        self.name = input('Name: ')
        self.age = int(input('Age: '))

    # Creating an instance method to display data of instance variables
    def displaydata(self):
        print('Roll No: %d' %(self.rollno))
        print('Name: %s' %(self.name))
        print('Age: %d' %(self.age))


# Creating object x of the class Record
x = Record()

# Call inputdata() of object x to store data in its instance variables
x.inputdata()

# Call displaydata() of object x to display data of its instance variables
x.displaydata()

Output

Roll No: 1
Name: Thomas
Age: 16
Roll No: 1
Name: Thomas
Age: 16

Let's take another example that can be coded as class and object in python using instance variable and instance methods.

Suppose we want to store the details of a student like a roll no, name, class, marks obtained in three different subjects (English, Maths, Computer), total and percentage obtained.

For the above question, we will create a class called Student with the following instance variables:

  • roll: for storing roll no of the student.
  • name: for storing name of the student.
  • cls: for storing class of the student.
  • eng: for storing marks obtained in english.
  • math: for storing marks obtained in math.
  • comp: for storing marks obtained in computer.
  • total: for storing total marks obtained.
  • per: for storing total percentage obtained.

We will also create three instance methods inside the Student class for processing the instance variables, and they are:

  • inputdetails(): for storing information in the instance variables.
  • calculate() for calculating and storing the total and percentage obtained.
  • display(): for displaying the information stored in the instance variables on the screen.

Example 2

class Student:

    # Creating instance variables using constructor method
    def __init__(self):
        self.roll = 0
        self.name = ""
        self.Class = ""
        self.eng = 0
        self.math = 0
        self.comp = 0
        self.total = 0
        self.per = 0

    # Creating an instance method to store data in instance variables
    def inputdetails(self):
        self.roll = int(input('Roll No: '))
        self.name = input('Name: ')
        self.Class = input('Class: ')
        self.eng = int(input('English: '))
        self.math = int(input('Math: '))
        self.comp = int(input('Computer: '))

    # Creating an instance method to calculate and store total and percentage
    def calculate(self):
        self.total = self.eng + self.math + self.comp
        self.per = (self.total/300)*100

    # Creating an instance method to display data of instance variables
    def display(self):
        print('Roll: %d' %(self.roll))
        print('Name: %s' %(self.name))
        print('Class: %s' %(self.Class))
        print('English: %d' %(self.eng))
        print('Math: %d' %(self.math))
        print('Computer: %d' %(self.comp))
        print('Total Marks: %d' %(self.total))
        print('Percentage: %d' %(self.per))


# Creating object x of the class Record
x = Student()

# Call inputdetails() of object x to store data in its instance variables
x.inputdetails()

# Call calculate() of object x to calculate total and percentage obtained
x.calculate()

# Call display() of object x to display data of its instance variables
x.display()

Output

Roll No: 1
Name: Thomas
Class: VIII
English: 78
Math: 92
Computer: 84
Roll: 1
Name: Thomas
Class: VIII
English: 78
Math: 92
Computer: 84
Total Marks: 254
Percentage: 84