- Python static method belongs to the Class.
- They are used to create utility methods for the class.
- The static method is called from the Class reference.
- They can’t change the state of the object since they belong to the class.
- In fact, a static method doesn’t have access to the class attributes.
- The static method is like a function in a Python script but inside the class body.
- We can call a static method from either the class reference or the object reference. If foo() is the static method in Class Utils, we can call it as
Utils.foo()as well as
Why do we need Python static methods?
The static methods provide a way to divide the utility methods into separate sub-modules.
Let’s say we have a Python module for utility methods for string, list, and tuple.
We can create static methods in the separate classes for better understanding and ease of use.
class ListUtils: @staticmethod def reverse(l): pass @staticmethod def clear(l): pass class StringUtils: @staticmethod def reverse(l): pass @staticmethod def upper(l): pass class TupleUtils: @staticmethod def reverse(t): pass @staticmethod def shuffle(t): pass
If we have to reverse a List, we will call
ListUtils.reverse() method. If we have to shuffle the tuple elements, we will call
TupleUtils.shuffle() static method.
How to Create a Static Method in Python
Python provides two ways to create a static method.
- staticmethod() method
- @staticmethod function decorator
1. Using staticmethod() function
The staticmethod() is useful when you want to create a static method defined in the class. Note that the method shouldn’t have the self argument.
class StringUtils: def to_uppercase(s): return str(s).upper() StringUtils.upper = staticmethod(StringUtils.to_uppercase) print(StringUtils.upper('Python')) # PYTHON
If we try to call the to_uppercase() method from StringUtils object, it will raise an error as “the method takes 1 positional argument but 2 were given”.
su = StringUtils() try: print(su.to_uppercase('Python')) except TypeError as te: print(te) # Output # to_uppercase() takes 1 positional argument but 2 were given
We can call the to_uppercase() method from the class reference though.
print(StringUtils.to_uppercase('Python')) # PYTHON
2. Using @staticmethod annotation
This is the recommended way to create a static method. We just need to annotate the method with the @staticmethod decorator.
class MathUtils: @staticmethod def multiply(a, b): return a * b print(MathUtils.multiply(10, 5)) # 50
This way of creating static method is very simple to use, readable, and maintainable.
Python Static Method vs Class Method
- Python class method can access class variables but static method can’t access class variables.
- A class method requires the first formal parameter to bind to the class. A static method can be present without any parameters.
- We use @classmethod to create class methods. We use @staticmethod to create static methods.
class Test: x = 10 @classmethod def foo(cls): print(cls.x) @staticmethod def bar(): # Unresolved reference error for class variable x # print(x) pass
Python Static Method vs Instance Method
- The instance method in the class can access instance variables as well as class variables. The static method can’t access class variables or instance variables.
- We require the “self variable” as the formal parameter for the instance method. There is no such restriction with the static method.
- We use @staticmethod decorator to create a static method. We don’t need any decorators to create instance function.
class Test: x = 10 def func(self): print(type(self)) print(self.x)
Benefits of Static Methods in Python
The static methods are beneficial to group utility methods inside a class boundary. Otherwise, it’s just like a normal function in a python script.