Introduction to Python @staticmethod
The following article provides an outline for Python @staticmethod. A staticmethod is a method that is basically bound to the class it represents and not any object of any class. It is a method with no right to access or modify class state. The @staticmethod is a built-in function and simply returns a static method. These static methods are similar to any other class methods and are bound to a class. These staticmethod does not require class instance creation, that is why they are not dependent on a state of object. These static methods are usually used when we have to process any data related to class and not of instance.
Syntax:
The standard syntax for @staticmethod decorator is as follows:
@staticmethod( function )
Here, the staticmethod is the keyword used in the syntax and the function is required for proper execution of method. It doesn’t have to be always be executed using a function, we can implement the @staticmethod without function.
The parameter we pass here is a function which we intend to convert into a static method by passing the function as a parameter. And for the return value, the staticmethod returns a static method of the function that we passed.
How @staticmethod Decorator works in Python?
- The @staticmethod is a Python Descriptor. When called, the python will invoke the __get__ method when have to access the object. This is where the translation begins in python.
- This staticmethod is also similar to @classmethod. When we initiate the staticmethod in our code it passes the power to the function that we have passed and execution turns to the function.
- One of the major applications of using staticmethod is to create multiple utility functions.
Examples of Python @staticmethod
Given below are the examples mentioned :
Example #1
For our first example, we will simply call for a print statement which will include the @staticmethod. Here, we will not pass any function for decorator.
Code:
class State:
@staticmethod
def city():
print("Pune - Simple Example to Demonstrate working of @staticmethod, without function.")
s=State()
s.city()
Explanation:
Started with creating a class named State, and called the @staticmethod decorator within the first class. Then we have a simple function defined named city and within this function is our simple print statement, which will print a simple message on successful execution.
Then we have assigned our first class with a simple variable as s. And at the end, we have s.city(), which means we call for the city() function which is part of state class. The s.city() can be understood as, call the city function, as a part of s, where s is the state class.
Output:
As you can see, our print statement has been printed using the @staticmethod.
Example #2
We will implement the @staticmethod constructor along with the function. So here we will pass a function and check the output.
Code:
class Sample:
def Ex():
print("This is an example for @staticmethod with function")
Ex = staticmethod(Ex)
Sample.Ex()
Explanation:
Similar to our first example, we have a simple class and a function defined within the class. We aim to get the print statement as output through @staticmethod. We then have another variable to call the staticmethod, and we have passed the function we created. Finally, we have called the function Sample.Ex(), meaning call for Sample class which holds the Ex() function. The output is expected to be the value that the function holds.
Output:
As expected, the output is “This is an example for @staticmethod with function” which was our intend. Here we saw how a staticmethod works when passed with a function. Earlier we saw working of staticmethod without passing any function.
Example #3
We will pass a age which will be checked and defined it the person is adult or not. The code for the program of age and adult function is as follows.
Code:
from datetime import date
class Ex_Per:
def __init__(self, age):
self.age = age
@staticmethod
def isAdult(age):
return age > 18
n_age=33
sample = Ex_Per(n_age)
print (sample.age)
print (Ex_Per.isAdult(n_age))
Explanation:
Our main aim here is to print the age of the person and to print the result in Adult function. Started with importing date from datatime module. Then we have our class Ex_Per, which has the definition age. Then we have our @staticmethod which has our function as an adult, and our function has the responsibility to check if the given age is greater than 18 or not. Moving on, when then have declared a simple variable to hold the age, which is 33 for this example.
Then we have another variable to hold our class and age. Finally, with our first print statement, we print the age of the person as sample.age. This sample.age can be read as sample which holds the class Ex_Per and within that the age function. And our second print statement, we have Ex_Per.isAdult(n_age). Our last line will simple print either true or false based on the result of is Adult function. The a_age here is the variable that holds our age which is 33.
Output:
As you can see, we tried the same example with two different ages. When we executed the code with age as 11, it printed False for being adult. 11 is smaller than 18, so the person with age of 11 is not adult. Then we again executed the code with age as 33, and as 33 is bigger than 18, the result for is Adult is True. This program was example of implementation of @staticmethod.
Conclusion
The @staticmethod is just another built in function, that does not take any parameter. The @staticmethod decorator is employed here. The static method do not take any kind of parameter. And it cannot access or modify the state class. @staticmethod is used in utility function creation. And the static method is unaware of anything that happens within a class.
Recommended Articles
This is a guide to Python @staticmethod. Here we have discussed how @staticmethod decorator works in python with programming examples. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses