From this tutorial, you will get to learn about the Python Static method. It is one of the essential concepts to use while you are learning OOP programming with Python.
Note: The syntax used in the below section is for Python 3. You may change it to use a different version of Python.
Python Static Method
Static methods are methods that operate at the class level rather than an object of that class. They are good for utility tasks that do not need to access or modify the state of an object. Let’s learn more about them in later sections.
To master Python OOP concepts – Read Python Class Method
What is a static method in Python?
A static method is a method that belongs to a class but does not need an instance of that class for calling it from the code. This means that you can call a static method without creating an object of the class. They make a good utility function for the class.
To declare a method as static, Python provides the
@staticmethod decorator. Alternatively, you can create a static method by calling the
It is important to realize that you don’t confuse the static method with the Python class method. Some of the main differences, we tried to collate here:
– Static method can’t access class attributes
– They can’t modify the state of the class or perform any factory operation.
– The static method serves to provide the utility functions for the class.
How does the Python Static method work?
When you create a class, you usually define a few methods and properties. They may or may not have access to the class instance.
The three types of methods depending upon their access are the Instance Method, Class Method, and Static Method. This tutorial will cover the application of Static Methods.
It is a method that does not have access to the class state. In other words, the method confines itself and cannot change the properties of the class instance without any workaround.
How to use the static method in Python programs?
You can make use of “
@staticmethod” decorator to define a method as static with the following syntax:
class class_name: @staticmethod def object_behaviour: //Code to be executed
Alternatively, you can follow the below syntax to denote a static method in Python.
- To organize your code: Static methods can help you to organize your code by grouping related methods together. This can make your code more readable and maintainable.
- To improve performance: Static methods can sometimes improve performance, as they do not need to create an object of the class each time they are called.
- To avoid name clashes: If you have two methods with the same name in different classes, you can use static methods to avoid name clashes.
- To create utility methods: Static methods are good for utility purposes. They work standalone without interfering with the class properties.
- Less flexibility: Static methods cannot access or modify the state of an object, which can limit their flexibility.
- Can be difficult to debug: Static methods can be difficult to debug, as they do not have an associated object.
Overall, static methods can be a useful tool for organizing and improving your Python code. However, it is important to use them carefully and to be aware of their limitations.
Static method examples
Let’s now go through some practical usage of the static methods in Python. Check out the below examples.
Function returning a value as static:
Here is a simple program to demonstrate static methods.
class Math: @staticmethod def Multiply(one, two): return one * two math = Math() if(12*72 == math.Multiply(12, 72)): print("Equal") else: print("Not Equal")
Save the above code as “staticmethod.py” and execute. The output will come as:
A method as static:
Check another program to use the built-in
staticmethod() function so that a method can behave as a static method in Python.
class Person: pass def is_adult(age): """Returns True if the given age is 18 or greater, False otherwise.""" return age >= 18 # Create a static method using the staticmethod() function. Person.is_adult = staticmethod(is_adult) # Call the static method. is_adult = Person.is_adult(18) # Print the result. print(is_adult) # True
Save the above code as “builtinstaticmethod.py” and execute. The output will come as True.
A method to print the current time:
from datetime import datetime class DemoClass: @staticmethod def get_current_time(): return datetime.now() print(DemoClass.get_current_time())
The above code will print the current time to the console. The get_current_time() method is a static method, so it can be called without creating an instance of the DemoClass class. This is useful because the current time does not depend on any state of the DemoClass object.
Let’s now summarize the learnings from this tutorial:
- Static methods are class-level methods that are self-sufficient for execution.
- This means that a static method can be called without an object for that class.
- Static methods cannot access or modify the state of an object, as they are not bound to it.
- Static methods are often used for utility methods that do not need to access or modify the state of an object.
- To create a static method in Python, you use the