Introduction to Python @staticmethod
The following article provides an outline for Python @staticmethod. A staticmethod is a method which is basically bound to the class it represents and not any object of any class. It is a method which has no right to access or actually 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.
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 :
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.
print("Pune - Simple Example to Demonstrate working of @staticmethod, without function.")
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.
As you can see, our print statement has been printed using the @staticmethod.
We will implement the @staticmethod constructor along with the function. So here we will pass a function and check the output.
print("This is an example for @staticmethod with function")
Ex = staticmethod(Ex)
Similar to our first example, we have a simple class and a function defined within the class. Then within our function is a simple print statement. Our aim is to get that print statement as output through @staticmethod. We then have another variable to call the staticmethod and we have passed the function we created earlier. Finally we have called the function as Sample.Ex(), meaning call for Sample class which holds the Ex() function. The output is expected to the the value that the function holds.
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.
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.
from datetime import date
def __init__(self, age):
self.age = age
return age > 18
sample = Ex_Per(n_age)
Our main aim here is to print the age of the person and also to print result is 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 within itself has our function as is 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.
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.
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.
This is a guide to Python @staticmethod. Here we discuss how @staticmethod decorator works in python with programming examples. You may also have a look at the following articles to learn more –