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
selfparameter 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
clsparameter 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
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.