. Advertisement .
..3..
. Advertisement .
..4..
A static class may be utilized as a handy container for groups of methods that only work with input arguments and don’t need to set or get internal instance fields. Still, it can’t be inherited or instantiated. Plus, making a class static cannot be done directly. So how can you create a Python static class?
You are at the right place! This tutorial will show you how to perform this task in three different methods. We also include many examples for you to use as references. Check it out and follow our instructions to complete your task.
Method 1: Create Static Class With The @staticmethod Python Decorator
The built-in decorator @classmethod
is an expression that is evaluated after a function is identified. The outcome of that assessment shadows the definition of your function.
You can create the static class’s variables and methods static in order to implement it. To do this, you may construct static methods in a class utilizing the decorator @staticmethod
. A decorator function is a unique feature specified before another function and receives the entire function as parameters.
Here is a complete example for your reference.
class abc(object):
@staticmethod
def static_method():
print("Hi,this tutorial is about static class in python")
abc.static_method()
Output:
Hi,this tutorial is about static class in python
Keep in mind that this technique only functions with Python versions 2.6 and later.
Method 2: Create Static Class With The @classmethod Python Decorator
It is impossible to pass an implicit initial parameter to a static method. The method @classmethod
can turn a method static for the class rather than the class’s objects. Compared to the decorator @staticmethod
, this one has various advantages. Additionally, it can change the class state and is compatible with subclasses.
However, it necessitates the inclusion of a required parameter in the cls method. The code below will help you better understand.
class abc(object):
@classmethod
def static_method(cls):
print("Hi,this tutorial is about static class in python")
abc.static_method()
Output:
Hi,this tutorial is about static class in python
Method 3: Create Python Static Class With A Module File
Creating a module file is likely the most effective method for implementing a static class in Python. A module can be imported, and its name can be used to access its functionalities. Notice that it is not inheritable and will not need objects to be created.
You will better understand with the example below.
def static_method():
print("Hi this is the content of the module file")
In the Python script, the code above can be seen as a basic function and is importable as a module.
For instance, if the Python script mentioned above is called abc.py, you can take the actions displayed below.
import abc
abc.static_method()
Output:
Hi,this tutorial is about static class in python
The Bottom Line
A static class is a useful tool for programmers. You can create a Python static class by making a class’s variables and methods static. Above, we have explained different methods for performing this task. Follow along, and you will soon master these techniques.
Bonus: Suppose you still do not fully understand what a Python class variable is; check out this guide to learn how to define static variables in Python.
Leave a comment