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
selfparameter 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
clsparameter that refers to the class. - Static methods: General utility methods that don’t use the object or the class state. They take neither
selfnorcls.
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
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).

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
defkeyword to define an instance method. - Use
selfas the first parameter of the instance method. Theselfparameter refers to the current object. - Use the
selfparameter 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:
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
nameandagein theStudentclass. - Next, create an instance method
show()to print the student’s name and age. - Finally, create objects of the
Studentclass and call the instance method on them.
Let’s see how to call the instance method show() to display each student’s details.
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.
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.
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:
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.
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
delstatement - 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.
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().
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
@classmethodor@staticmethod. - Call an instance method with
object.method(). Python passes the object asselfautomatically. - 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.methodordelattr(ClassName, 'method').
