PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C 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: August 28, 2021 | 7 Comments

In Python object-oriented programming, when we design a class, we use the instance methods and class methods.

Inside a Class, we can define the following two types of methods.

  • Instance methods: Used to access or modify the object state. If we use instance variables inside a method, such methods are called instance methods. It must have a self parameter to refer to the current object.
  • Class methods: Used to access or modify the class state. In method implementation, if we use only class variables, then such type of methods we should declare as a class method. The class method has a cls parameter which refers to the class.

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 dynamically add or delete instance methods in Python

Table of contents

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

What is Instance Methods in Python

If we use instance variables inside a method, such methods are called instance methods. The instance method performs a set of actions on the data/value provided by the instance variables.

  • A instance method is bound to the object of the class.
  • It can access or modify the object state by changing the value of a instance variables

When we create a class in Python, instance methods are used regularly. To work with an instance method, we use the self keyword. We use the self keyword as the first parameter to a method. The self refers to the current object.

Any method we create in a class will automatically be created as an instance method unless we explicitly tell Python that it is a class or static method.