Welcome to the methods module in Python. Everyone is aware of methods that it is called to perform some operation like area calculation of circle. First, we define the method using a def command in python. Later on, we call it with some arguments and receive some values through the return statement.
But Python is an object-oriented programming language. Everything is treated here is as Objects. In Object-Oriented Programming, we define a class. The class has attributes and few methods encapsulated in it. We can call these methods of the class in programming as per requirement.
So, there are 3 ways to write method and call these methods in Python. In this article, we will see different method call available in python and how to decide which one you should choose as a developer.
Let’s begin with different methods.
1) Instance Method:
The instance method call is used widely for methods. The instance object needs to be passed as the first argument of the method. This method takes the first parameter as “self” which points to the same instance of the class for which it is called. “self” parameter is used to access class attributes and other methods. It is called through an object of the class; therefore, it can modify the object state of that class instance.
Since this method is bound to an object, it is also called “bound method calls”. The instance method is powerful as it can change the object’s state as well as the class’s state. Really? But how?
The answer is self. _class_ attribute. Class’s state also can be modified using this attribute. So, the Instance method is very powerful as it can modify the state of both object and class. Let’s see below an example of how to go for the Instance method.
def instancemethod(self, name , age):
return 'instance method called', self
student1 = Student()
Here is the output where you can see the age for student1 instance has been printed.
Here, you can notice that the instance method is having 3 parameters self, name, and age. The First parameter always should be self. It can have multiple arguments. When the instance method is called, Python internally replaces the “self” argument with the instance object, student1. Now, self will refer to object student1 of class Student.
Let’s see what happens when we call the instance method without creating an object:
We have received an error as we have not created any object of the student class. Instead, we are calling the instance method directly as student.instancemethod instead of student1.instancemethod. Hence, we should always create an object instance of the class then call the instance method with object.instancemethod.
Now, there is one more way to call the instance method:
('instance method called', <__main__.Student at 0x2161cdb6310>)
Here, we are manually passing the object student1 as a parameter. Python internally passes the object as self-parameter while the instance method calls. When we say student1.instancemethod(‘ashruti’,22), Python internally executes Student.instancemethod(student1, ‘ashruti’,22). Student1 object has been passed as the first ‘self’ parameter.
2) Class method:
The class method accepts the “cls” parameter instead of the “self” parameter of the instance method. This “cls” parameter refers to the class itself, not the object instance.
The class method can modify the class state irrespective of objects. The class state modification applies to all instances of the class. Let’s see how to define the class method using the @classmethod decorator and call.
def classmethod(cls, name, year):
age = date.today().year-year
You can see how the class method is called and modified class attribute “age”. The year has been passed as an argument and the class method calculated age using today() function.
Here, we can observe that there is no need to create an object of class as we did in the instance method. We directly called class method using class_name.class_method_name. During the call of a class method, we did not write the cls parameter. Python automatically accepts the first parameter as class “cls”.Since the class method is not associated with any object instance, it is considered an unbound method call.
3) Static Method:
This method does not accept any self (instance object) or cls(class) parameter. But It can have any number of other arguments. Static methods should be defined using the @staticmethod decorator.
A static method can’t modify the state of an object or class as it doesn’t accept self or cls parameters.These methods are used to write the utility method. Like if some operation needs to be performed which is not related to an instance or class attribute modification, static methods are used.
Let’s see how to define a static method:
Here, you can notice that the static method is used to operate on the data which has been passed as arguments. It did not work upon object or class instance.
There is no need to create an object of class like student1 as we did in the instance method. We have directly called the static method with class_name.static_method. Therefore, it is also an unbound method call like class method.
How to decide which method should be chosen
Now, you must be wondering which method should be chosen. So, there are no hard and fast rules in Python. Here are some points, one should consider about the requirement, before deciding which method is best suited.
Static Method is like regular functions but belongs to the class’s namespace. We do not require first to create a class instance then test it.
Future maintenance of static methods is also easy. Static methods are generally used to write utility methods. Class methods are used generally to write factory methods that return class objects for different use cases.
It does not mean you need to always choose static methods over class and instance. The developer should choose a class method when to want to perform operations on class attributes. Similarly, the instance method should be chosen when object instance-specific data or operations are required.
Choosing the static method, class method, or instance method while writing code ensures that future developers use your code or method with the same purpose you have created it originally. There should not be accidental use of the incorrect methods.
Let say the developer has decided to calculate the area of a circle with the static method for a given radius. He wants to call this method for different objects through the instance method which internally calls this static method. Because the area calculation logic is not going to change based on object or class instance. So, a developer can choose to calculate the area as a static method. He can define other instance methods to calculate the area for different object instances with different radius.
Let say the developer has written static method and instance method. The future developer on the same programming will use the methods with the same intention as the original developer has. Python enforces static method restriction at run time.
Python has provided these three methods to enhance flexibility. The developer intent about class design is enforced by static, class, and instance methods. Learning and applying methods design concepts of Python in code, makes your code powerful, easy to maintain, and clean.
Hope you enjoyed learning static, class and instance method. We will explore more in the next articles.