Difference between Class Method, Static Method, and Instance Method
In this tutorial, we will learn about the class method, static method, and instance method. These methods are the core concepts of object-oriented programming in Python. Every Python enthusiast should be familiar with these methods and how to use them as per the requirements. Let's briefly introduce instance, class, and static methods.
What are Instance Methods?
As its name suggests, the instance methods are bound to the class instance and perform a set of actions on the data/value given by the object (instance) variables. If we use the instance variable inside the methods, these methods are called instance methods. It can modify the object state. The self keyword is the first parameter to work with the instance method, and the self refers to the current object.
Name: Craig Department: IT
We have defined the constructor that creates the instance variable using the Python object in the above code. In the show() function, we accessed both variables using the self keyword. We can access and modify the instance variable using the below code.
Example - 2
Name: Craig Department: IT Name: Mathew Department: HR
We can see that we accessed the instance variable and changed its value. In the show() function, the instance method printed the updated values.
The class methods are bound to the class, not to the instance. It can modify the class state means it can change class configuration globally. It can access only the class variable. The class methods are used to create the factory methods. The syntax of class methods is different; instead of taking self parameter, they accept cls as a parameter that points to the class. It can't modify the instance state. However, the changes made by the class method reflect all instances of the class. The @classemethod decorator or classmethod() defines the class methods. Let's understand the following example.
Name: Craig Department: IT Salary: 25000 Name: Craig Department: IT Salary: 45000
In the above code, we have defined the class variable name salary and the class method change_salary(). The change_salary() function accessed the class variable salary, and it used to modify the salary for all the employees. We have done the same in the object creation part.
The Static methods neither use self nor cls parameter; general utility methods perform the task in isolation. Static methods in Python are similar to those found in Java and C++, and they can't modify the behavior of the class or instance. The @staticmethod decorator or staticmethod() method defines the static methods. Let's see the below example. The static methods can be accessed using the class name and instance of the class.
Inside static method 100 Inside static method 100
We have created a static method that takes x as an argument in the above method. We calculated multiplication with itself, and we accessed the static method using the class name and the instance.
Let's understand the difference between class, instance, and static methods.
Class Methods vs. Static Methods vs. Instance Methods
We will describe some essential differences between these methods using some parameters. These differences will help you get more understanding of oops concepts.
Class Bound and Instance Bound
We have discussed some essential differences between the class, static, and instance methods. We have also explored examples and shown the different scenarios to use these methods.