Instance Method in Python with Example

When we define a function inside a class definition, it is called instance method in Python. It belongs to a specific instance of a class, which is passed as the first argument in the method definition.

By convention, this argument is called self, which refers to the current instance of the class. Every instance method must have self as its first parameter.

However, while calling an instance method, we do not need to explicitly pass the instance of a class as an argument. Python implicitly takes care of it for us.

Syntax to Define Instance Method in Python


In Python, we can declare an instance method in a class by using the following general syntax. It is as:

class class_name:
    def method_name(self, parameter_list):
        # Method body - define what the method does.
        # Here, we can access instance attributes and perform operations.
        
    # Here, we can define additional methods and class members.

(1) In the above syntax, class is a keyword which we use to define the class in Python. The class_name defines the name of class.

(2) The statement def method_name(self, parameter_list) defines an instance method in a class. In the method definition, def is a keyword used to define a method. The method_name represents the name of method, which is followed by parentheses containing self and parameter_list.

(3) The self parameter is a convention in Python that represents the current instance of the class. It must be the first parameter in any instance method. It allows us to access instance attributes (i.e. instance variables) and behaviors (i.e. other methods) of the object within the method.

(4) The parameter_list represents the list of parameters that allows us to pass data and perform operations with instance attributes inside the instance method.

(5) The method body is an indented block below the method declaration contains the code that defines what the method does. We can access instance attributes using self keyword and perform various operations within the method.

Simple Instance Method Example Program


Let’s start with a basic example in which we will declare an instance method in a class and access it.

Example 1:

class MyClass:
  # Instance method definition.
    def ins_method(self):
        print("I am an instance method")

# Outside class definition.
# Create an object of class MyClass.
obj = MyClass()

# Accessing instance method using object reference variable obj.
obj.ins_method()
Output:
       I am an instance method


2. When we declare variables inside the __init__() constructor method, it is called instance variables or instance attributes of the class. Let’s take an example in which we will access instance variables within an instance method.

Example 2:

class MyClass:
  # Constructor declaration.
    def __init__(self):
        self.x = 20 # Instance variable
        self.y = 40 # Instance variable

  # Instance method definition.
    def m1(self):
      # Accessing instance variables inside an instance method.
        print("Value of x = ", self.x)
        print("Value of y = ", self.y)

# Outside class definition.
# Create an object of class MyClass.
obj = MyClass()

# Accessing instance method using object reference variable obj.
obj.m1()
Output:
       Value of x =  20
       Value of y =  40

3. When we declare variables outside the __init__() method, but inside the class definition, it is called class variables or static variables. Let’s take an example program in which we will access class variables inside the instance method in Python.

Example 3:

class MyClass:
    x = 20 # class variable
    y = 30 # class variable

  # Instance method definition.
    def m1(self):
      # Accessing class variables using class name inside instance method.
        print("Value of x = ", MyClass.x)
        print("Value of y = ", MyClass.y)

# Outside class definition.
# Create an object of class MyClass.
obj = MyClass()

# Accessing instance method using object reference variable obj.
obj.m1()
Output:
       Value of x =  20
       Value of y =  30


Example 4:

class MyClass:
    def m1(self):
        print("I am m1 method")
    
    def m2(self):
      # Calling an instance method from inside another instance method.
        self.m1()
        print("I am m2 method")


# Outside class definition.
# Create an object of class MyClass.
obj = MyClass()

# Accessing instance method using object reference variable obj.
obj.m2()
Output:
       I am m1 method
       I am m2 method

Instance Method with Parameters in Python


An instance method in Python can accept additional parameters besides self parameter. These parameters allow us to pass data and perform operations with the instance attributes. Let’s take a simple example program based on it.

Example 5:

class Calculator:
  # Declare instance methods for addition, subtraction, multiplication, and division.
    def sum(self, x, y):
        z = x + y # Local variable.
        print("Addition: ", z)
    
    def sub(self, x, y):
        z = x - y
        print("Subtraction: ", z)
    
    def prod(self, x, y):
        z = x * y
        print("Multiplication: ", z)
    
    def div(self, x, y):
        z = x / y
        print("Division:  ", z)

# Outside the class definition.
# Instantiation of class.
c = Calculator()

# Calling instance methods in sequence.
c.sum(20, 30)
c.sub(30, 40)
c.prod(40, 50)
c.div(50, 20)
Output:
       Addition: 50
       Subtraction: -10
       Multiplication: 2000
       Division: 2.5

Instance Method with Return Statement


Let’s take a simple example program in which we will define an instance method with a return statement in Python.

Example 6:

class Calculator:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def sum(self):
        z = self.x + self.y
        return z # Returning a value to method call.
    def sub(self):
        z = self.x - self.y
        return z # Returning a value to method call.
    def prod(self):
        z = self.x * self.y
        return z # Returning a value to method call.

# Outside the class definition.
c = Calculator(20, 10)

# Calling instance methods in sequence and storing returned values in variables.
sm = c.sum()
sb = c.sub()
pd = c.prod()

print(sm)
print(sb)
print(pd)
Output:
       30
       10
       200

Some Advanced Example Programs


Example 7:

class MyClass:
    x = 20 # class variable
    def __init__(self):
        self.x = 30 # instance variable

    def m1(self):
        print(self.x) # Accessing instance variable.
        print(MyClass.x) # Accessing class variable.

# Creating an instance of class MyClass.
c = MyClass()
c.m1()
Output:
       30
       20

Example 8:

class MyClass:
    x = 20 # class variable
    def __init__(self):
        self.x = 30 # instance variable

    def m1(self):
        x = 40 # local variable
        print(x)
    
    def m2(self):
        print(self.x)
        print(MyClass.x)
        self.m1()

# Creating an instance of class MyClass.
c = MyClass()
c.m2()
Output:
       30
       20
       40

Advantages of Instance Method in Python


There are the following significant advantages of using instance methods in Python. They are as:

  • Instance method allows us to encapsulate the behavior associated with an object within the object itself.
  • We can define instance methods in a class and reused across multiple instances of that class. This improves code reusability, as we can write a method once and apply it to various objects, reducing redundancy in our code.
  • Instance methods break down the code of complex tasks into smaller, manageable methods. Each method can focus on a specific object’s behavior, making the code easier to understand and maintain.
  • We can access and manipulate the attributes of an instance inside an instance method.
  • Instance method improves code organization. By grouping related functionality within instance methods, our code becomes more organized and follows a logical structure. This organization enhances code readability and maintainability.
  • Debugging is often more straightforward when using instance methods because we can examine independently the behavior of each object.
  • Instance method simplifies the process of identifying and fixing issues in the program code.

In this tutorial, we have explained the instance method in Python with the help of various examples. Hope that you will have understood how to declare an instance method inside a class and practiced all example programs.
Thanks for reading!!!

Please share your love