As we have discussed in Tutorial#56, there are two types of methods i.e., static and class. In that tutorial, our primary focus was on class methods. From this tutorial, you will get to learn about the Static method. It is one of the important concepts to use while you are learning OOPs programming with Python. A static method is very much easy to understand if we are familiar with class methods. It is also easier to implement than a class method because it can be accessed without any object. However, we can also access it using a class or any instance.
Figure 1: class method in Python
We know that a static method in Python is treated differently than in other languages. Static methods in Python are extremely similar to python class methods, but the difference is that a static method is bound to a class rather than the objects for that class.
To define a static method, we use @staticmethod decorator, which is a built-in decorator. Also, there is no need to import any module to use decorators. Using a static method in a class, we permit it to be accessed only by the class objects or inside the class.
There are few limitations related to static methods, such as:
Static methods do not require any knowledge related to the class that they are built-in. They are only formed in a class so that only the class instances can access them. We can use a static method for simple functionality that is mostly not related to the class. For example, we want to add two numbers together, but we do not want our method to be used elsewhere, other than the class, or through its instances, so we will create a static method. It will not require any information related to class as it only requires the numbers it has to add, but it will still fulfill its purpose as it is like a personal method that only the class has access to.
Most of the functionalities of the static methods can be performed using a class method. However, we prefer the static method, at places where it could work to make our program more efficient and faster as we do not have to pass self as a parameter, so the efficiency of the program increases.
In Python static methods can be created using @staticmethod.
class Student:
@staticmethod
def myfunc():
//Code to be executed
Alternatively, we can follow the below syntax as well:
staticmethod(class_name.method())
Using @staticmethod annotation is actually a better way to create a static method in Python as the intention of keeping the method static is clear.
Static methods have a very clear use-case. When we need some functionality not for an Object but with the complete class, we make a method static. This is advantageous when we need to create utility methods.
Finally, note that in a static method, we do not need the self or cls to be passed as the first argument.
class Employee:
no_of_leaves = 8
def __init__(self, aname, asalary, arole):
self.name = aname
self.salary = asalary
self.role = arole
def printdetails(self):
return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"
@classmethod
def change_leaves(cls, newleaves):
cls.no_of_leaves = newleaves
@classmethod
def from_dash(cls, string):
return cls(*string.split("-"))
@staticmethod
def printgood(string):
print("This is good " + string)
harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")
karan = Employee.from_dash("Karan-480-Student")
Employee.printgood("Rohan")
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here
Be the first person to comment!