Hey there! Welcome to Hostman! 🎉

Python Static Method

11.12.2023
Reading time: 4 min
Hostman Team
Technical writer

A static method in Python is a method that belongs to a class, not its instances. It does not require an instance of the class to be called, nor does it have access to an instance. Static methods in Python are declared using the @staticmethod decorator. This decorator tells the Python interpreter that the method is static and should be called on the class, not on an instance of the class.

Thus, static methods in Python can be used to perform operations that do not require access to the class instance or its attributes, meaning that they are essentially helper functions. To clarify what we're talking about, let's get straight to the practice.

Example #1

Imagine that we have a MathUtils class that contains a static method for calculating the factorial:

class MathUtils:
    @staticmethod
    def factorial(n):
        if n == 0:
            return 1
        else:
          return n * MathUtils.factorial(n-1)

Next, let's enter:

print(MathUtils.factorial(5))

120

We get the factorial of 5, which is 120. Here, the factorial static method does not use any attributes of the class instance, only the input argument n. And we called it using the MathUtils.factorial(n) syntax without creating an instance of the MathUtils class.

Static methods in Python can be used in classes, modules, and packages. If you define a function in a module and don't want it to be associated with an instance of a class or module, you can use the @staticmethod decorator to declare that function as static.

Example #2

Suppose we have a StringUtils module that contains a static method to check if a string is a palindrome. Let's write this code:

def is_palindrome(string):
   return string == string[::-1]

Here, the is_palindrome function is not associated with any instance of a class or module object, so we can use the @staticmethod decorator to declare it as static. To do this, let's extend our code in this way:

class StringUtils:
    @staticmethod
    def is_palindrome(string):
      return string == string[::-1]

Let's enter for verification:

print(StringUtils.is_palindrome("deed"))

True
print(StringUtils.is_palindrome("deer"))

False

That's correct, the first word is a palindrome, so the interpreter outputs True, but the second word is not, and we get False.

So, we can call the is_palindrome method through the StringUtils class using the StringUtils.is_palindrome(string) syntax instead of importing the is_palindrome function and calling it directly.

-

Another important difference between static methods and class instance methods in Python is that static methods cannot change the state of an instance. This means they can't change attribute values, which makes sense since they don't have access to the instance. If you want to change the instance state of a class, you need to use instance methods.

Example #3

Let's look at another example. Suppose we have a Person class that has an age attribute and a static is_adult method that checks the value against the age of majority:

class Person:
    def __init__(self, age):
        self.age = age

    @staticmethod
    def is_adult(age):
      return age >= 21

Next, let's create an age variable with a value of 24, call the is_adult static method from the Person class with this value and store its result in the is_adult variable, like this:

age = 24
is_adult = Person.is_adult(age)

Now to test this, let's enter:

print(is_adult)

True

Since the age matches the condition specified in the static method, we get True. In the example above, the is_adult static method accepts the age argument, but does not have access to the age attribute of the Person class instance, acting as an auxiliary function.

Conclusion

Static methods improve code readability and make it possible to reuse it. They are also more convenient when compared to standard Python functions. The convenience of static methods is that they don't require a separate import, as you have to do for functions. Thus, using static methods of a Python class can greatly simplify your code and your work with it. And, as you've probably seen from the examples above, they are quite easy to master.