Introduction
A key feature of Python is support for object-oriented programming. Static methods are essential in OOP and appear in most object-oriented languages.
In Python, static methods belong to a class instead of an instantiated object of that class. Additionally, there are multiple ways to call the method.
This article explains static methods and how to use them in Python.
Prerequisites
- Python 3 installed.
- An IDE or code editor to write the code.
- A way to run the code and see outputs (such as through the terminal or an IDE).
What Is a Static Method in Python
A static method is a type of method that belongs to a class but does not bind to a class or instance. Static methods do not rely on any class or instance data to perform a task.
Static methods perform a task in isolation from the class because they do not use implicit arguments such as self
or cls
. Therefore, a static method cannot modify the state of a class.
This method helps define utility methods through a logical connection to a class. The method call happens directly on the class object or through a class instance.
Instance Method vs. Class Method vs. Static Method
The main difference between Python methods is how the methods bind to a class. Class methods bind to a class through the cls
argument, instance methods bind to instances through the self
argument, while static methods do not bind to a class or an instance.
Note: The most common method type in Python is instance methods. Class and static methods are available since Python 2.2, but are not as common.
Below is a table with the key differences between the three methods.
Instance Method | Class Method | Static Method |
---|---|---|
Does not use a decorator or function. | Made with the @classmethod decorator or the classmethod() function. | Created with the @staticmethod decorator or the staticmethod() function. |
An instance is the implicit first argument through the variable self . | The class is an implicit first argument through the variable cls . | Does not pass any arguments to the method. |
Has access to and can modify the class and instance variables. | Has access to and can modify class variables. | Cannot access or alter the class or instance variables. |
All three Python methods belong to a class but have different functionalities and class access due to how they bind to a class.
Advantages of Static Method in Python
Using static methods in Python classes comes with several advantages. Static methods are:
- Safe to use. Since static methods cannot access the class or instance data, they cannot modify the class state. This method cannot change the behavior or affect a class unexpectedly.
- Predictable. A static method does not depend on the state of an instance or class. The behavior of a static method is predictable and does not change.
- Flexible. Calling a static method on a class improves code readability and lets a programmer know the method does not depend on a specific instance.
- An organization tool. Static methods provide a way to namespace code, preventing naming conflicts and improving organization.
How to Create Python Static Methods
Python offers two ways to create a static method inside a class. Below is a brief overview of each technique and example codes to show the differences.
Method 1: staticmethod()
The first way to create a static method is with the built-in staticmethod()
function. For example:
def myStaticMethod():
# Code that doesn't depend on class or instance
print("Inside static method")
class MyClass():
myStaticMethod = staticmethod(myStaticMethod)
The myStaticMethod
function resides outside the class and creates a static method by wrapping staticmethod()
.
Use the method in one of the following ways:
- Directly call the method on a class:
MyClass.myStaticMethod()
- Define an object instance and call the method:
obj = MyClass()
obj.myStaticMethod()
- Use the method as a regular function:
myStaticMethod()
In all three cases, the method prints the message to confirm.
Method 2: @staticmethod
The second way to create a static method is with the @staticmethod
decorator. For example:
class MyClass():
@staticmethod
def myStaticMethod():
# Code that doesn't depend on class or instance
print("Inside static method")
The decorator makes the method static without changing the structure.
To use the method, try one of the following ways:
- Directly call the method on a class:
MyClass.myStaticMethod()
- Instantiate an object and call the method on the object:
obj = MyClass()
obj.myStaticMethod()
- Reference the method through a variable and perform the call on the variable:
myFunction = MyClass.myStaticMethod
myFunction()
All three ways print the message from the static method.
Note: Learn how to use Python struct functions. Structs efficiently store and work with fixed-size data in memory.
Conclusion
After reading this guide, you know what a static method is in Python and how to use it.
Next, read about object-oriented design in our article about SOLID principles.