PYnative

Practice Coding

  • Python â–¼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises â–¼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQsâ–¼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilersâ–¼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Python Instance Methods Explained With Examples

Python Instance Methods Explained With Examples

Updated on: December 4, 2026 | 7 Comments

In Python object-oriented programming, when we design a class, we mostly use instance methods, along with class methods and static methods.

Inside a class, we can define the following three types of methods.

  • Instance methods: Used to access or modify the object’s state. If a method uses instance variables, it should be an instance method. It must have a self parameter that refers to the current object.
  • Class methods: Used to access or modify the class state. If a method uses only class variables, declare it as a class method. A class method has a cls parameter that refers to the class.
  • Static methods: General utility methods that don’t use the object or the class state. They take neither self nor cls.

Also, read Python Class method vs Static method vs Instance method.

After reading this article, you’ll learn:

  • How to create and call instance methods
  • How to modify instance variables and create new ones inside an instance method
  • How to call one instance method from another
  • How to dynamically add or delete instance methods in Python

Table of contents

  • What is Instance Methods in Python
  • Define Instance Method
  • Calling An Instance Method
  • Calling One Instance Method From Another
  • Modify Instance Variables inside Instance Method
  • Create Instance Variables in Instance Method
  • Dynamically Add Instance Method to a Object
  • Dynamically Delete Instance Methods
  • Summary

What is Instance Methods in Python

A method that works with instance variables is called an instance method. An instance method performs a set of actions on the data stored in the object’s instance variables.

  • An instance method is bound to an object of the class.
  • It can access or modify the object’s state by changing the values of its instance variables.

Instance methods are the most common type of method in Python classes. Their first parameter is self, which refers to the current object.

Any method you define in a class is an instance method by default, unless you explicitly mark it as a class method (@classmethod) or a static method (@staticmethod).

Instance method in Python
Instance method in Python

Define Instance Method

Instance variables are not shared between objects. Every object has its own copy of each instance attribute. Using an instance method, we can access or modify the attributes of the object that calls it.

Instance methods are defined inside a class, much like a regular function.

  • Use the def keyword to define an instance method.
  • Use self as the first parameter of the instance method. The self parameter refers to the current object.
  • Use the self parameter to access or modify the current object’s attributes.

You may use a different name instead of self, but it’s discouraged, because self is the standard convention in Python.

Let’s see an example that creates an instance method show() in the Student class to display student details.

Example:

class Student:
    # constructor
    def __init__(self, name, age):
        # Instance variable
        self.name = name
        self.age = age

    # instance method to access instance variable
    def show(self):
        print('Name:', self.name, 'Age:', self.age)Code language: Python (python)

Calling An Instance Method

We use an object and the dot (.) operator to call an instance method and run the code defined in it.

  • First, create the instance variables name and age in the Student class.
  • Next, create an instance method show() to print the student’s name and age.
  • Finally, create objects of the Student class and call the instance method on them.

Let’s see how to call the instance method show() to display each student’s details.

class Student:
    # constructor
    def __init__(self, name, age):
        # Instance variable
        self.name = name
        self.age = age

    # instance method access instance variable
    def show(self):
        print('Name:', self.name, 'Age:', self.age)

# create first object
print('First Student')
jessa = Student("Jessa", 14)
# call instance method
jessa.show()

# create second object
print('Second Student')
kelly = Student("Kelly", 16)
# call instance method
kelly.show()Code language: Python (python)

Output:

First Student
Name: Jessa Age: 14
Second Student
Name: Kelly Age: 16

Note:

When you call jessa.show(), Python automatically passes the object jessa as the self argument. So jessa.show() is the same as Student.show(jessa).

Inside any instance method, use self to access the data and methods of the object. Without self, the method can’t reach them.

An instance method can also change class-level state through self.__class__ (for example, self.__class__.school_name = 'XYZ School'). So an instance method can modify both the object and the class state.

Calling One Instance Method From Another

Inside a class, one instance method can call another using self.method_name(). Instance methods can also return values, just like regular functions.

class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def total(self):
        return sum(self.marks)

    def average(self):
        # call another instance method using self
        return round(self.total() / len(self.marks), 2)

    def report(self):
        print(self.name, '| Total:', self.total(), '| Average:', self.average())

s1 = Student('Emma', [80, 75, 90])
s1.report()

# s1.report() is the same as:
Student.report(s1)Code language: Python (python)

Output:

Emma | Total: 245 | Average: 81.67
Emma | Total: 245 | Average: 81.67

Modify Instance Variables inside Instance Method

Let’s create an instance method update() that modifies the student’s roll number and age when their details change.

class Student:
    def __init__(self, roll_no, name, age):
        # Instance variable
        self.roll_no = roll_no
        self.name = name
        self.age = age

    # instance method access instance variable
    def show(self):
        print('Roll Number:', self.roll_no, 'Name:', self.name, 'Age:', self.age)

    # instance method to modify instance variable
    def update(self, roll_number, age):
        self.roll_no = roll_number
        self.age = age

# create object
print('class VIII')
stud = Student(20, "Emma", 14)
# call instance method
stud.show()

# Modify instance variables
print('class IX')
stud.update(35, 15)
stud.show()Code language: Python (python)

Output:

class VIII
Roll Number: 20 Name: Emma Age: 14
class IX
Roll Number: 35 Name: Emma Age: 15

Create Instance Variables in Instance Method

So far, we’ve used the constructor to create instance attributes. But instance attributes don’t have to be created in the __init__() method. They can be created in any instance method. Let’s see how to create an instance variable inside a method.

Example:

class Student:
    def __init__(self, roll_no, name, age):
        # Instance variable
        self.roll_no = roll_no
        self.name = name
        self.age = age

    # instance method to add instance variable
    def add_marks(self, marks):
        # add new attribute to current object
        self.marks = marks

# create object
stud = Student(20, "Emma", 14)
# call instance method
stud.add_marks(75)

# display object
print('Roll Number:', stud.roll_no, 'Name:', stud.name, 'Age:', stud.age, 'Marks:', stud.marks)Code language: Python (python)

Output:

Roll Number: 20 Name: Emma Age: 14 Marks: 75

Note: The marks attribute exists only after add_marks() is called. Accessing stud.marks before that raises an AttributeError. For this reason, it’s good practice to create all instance variables in __init__() (for example, self.marks = None).

Dynamically Add Instance Method to a Object

Usually, we add methods to the class body when defining a class. However, Python is a dynamic language that allows us to add or delete methods at runtime. This is helpful in scenarios like these:

  • The class is in a different file, and you can’t modify its code.
  • You want to extend a class’s functionality for one object without changing the class structure that other code relies on.

Let’s see how to add an instance method to a Student object at runtime.

Example:

We add the method to one object only, so other instances don’t have access to it. We use MethodType() from the types module to bind the function to the object, so that self is passed automatically when it’s called.

import types

class Student:
    # constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # instance method
    def show(self):
        print('Name:', self.name, 'Age:', self.age)

# create new method
def welcome(self):
    print("Hello", self.name, "Welcome to Class IX")


# create objects
s1 = Student("Jessa", 15)
s2 = Student("Kelly", 16)

# Add instance method to s1 only
s1.welcome = types.MethodType(welcome, s1)
s1.show()

# call newly added method
s1.welcome()

# s2 doesn't have the new method
print(hasattr(s2, 'welcome'))Code language: Python (python)

Output:

Name: Jessa Age: 15
Hello Jessa Welcome to Class IX
False

Tip: To add a method to all objects of a class, assign the function to the class instead: Student.welcome = welcome. No MethodType() is needed in that case.

Dynamically Delete Instance Methods

We can also delete methods dynamically. In Python, there are two ways to delete a method:

  • By using the del statement
  • By using the delattr() function

Important: Methods defined in the class body belong to the class, not to the object. So you can’t delete them through an object. del emma.percentage raises an AttributeError, because emma itself has no attribute named percentage. To remove such a method, delete it from the class. This removes it for all objects.

By using the del statement

Example:

In this example, we delete the instance method percentage() from the Student class. If you try to call it after removing it, you’ll get an AttributeError.

class Student:
    # constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # instance method
    def show(self):
        print('Name:', self.name, 'Age:', self.age)

    # instance method
    def percentage(self, sub1, sub2):
        print('Percentage:', (sub1 + sub2) / 2)

emma = Student('Emma', 14)
emma.show()
emma.percentage(67, 62)

# Delete the method from the class using del
del Student.percentage

# Again calling percentage() method
# It will raise error
emma.percentage(67, 62)
Code language: Python (python)

Output:

Name: Emma Age: 14
Percentage: 64.5
AttributeError: 'Student' object has no attribute 'percentage'

By using the delattr() function

The delattr() function deletes the named attribute from an object or class. Use the following syntax to delete a method.

delattr(object, name)Code language: Python (python)
  • object: The object or class whose attribute we want to delete.
  • name: The name of the method to delete, as a string.

Example:

In this example, we add a method to one object at runtime and then delete it from that object using delattr().

import types

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

def welcome(self):
    print("Hello", self.name, "Welcome to Class IX")

s1 = Student("Jessa", 15)

# add method to s1 only
s1.welcome = types.MethodType(welcome, s1)
s1.welcome()

# delete the method added to the object
delattr(s1, 'welcome')
print(hasattr(s1, 'welcome'))Code language: Python (python)

Output:

Hello Jessa Welcome to Class IX
False

To delete a method defined in the class with delattr(), pass the class: delattr(Student, 'percentage').

Summary

  • An instance method works with an object’s data. Its first parameter is self, which refers to the current object.
  • Every method in a class is an instance method unless it’s decorated with @classmethod or @staticmethod.
  • Call an instance method with object.method(). Python passes the object as self automatically.
  • Instance methods can read, modify, and create instance variables, call other methods with self, and return values.
  • Use types.MethodType() to add a method to a single object at runtime.
  • Methods defined in the class belong to the class. Delete them with del ClassName.method or delattr(ClassName, 'method').

Filed Under: Python, Python Object-Oriented Programming (OOP)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Object-Oriented Programming (OOP)

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python Object-Oriented Programming (OOP)
TweetF  sharein  shareP  Pin

  Python OOP

  • Python OOP
  • Classes and Objects in Python
  • Constructors in Python
  • Python Destructors
  • Encapsulation in Python
  • Polymorphism in Python
  • Inheritance in Python
  • Python Instance Variables
  • Python Instance Methods
  • Python Class Variables
  • Python Class Method
  • Python Static Method
  • Python Class Method vs. Static Method vs. Instance Method
  • Python OOP exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com