◐ Shell
clean mode source ↗

Python Static Method Explained With Examples

In Object-oriented programming, at the class level, we use class methods and static methods.

  • Class methods: Used to access or modify the state of the class. if we use only class variables, we should declare such methods as a class method.
  • Static methods: A static method is a general utility method that performs a task in isolation. Inside this method, we don’t use instance or class variable because this static method doesn’t take any parameters like self and cls.

Also, read Python Class method vs Static method vs Instance method.

After reading this article, you’ll learn:

  • How to create and use the static methods in Python
  • Create staticmethod using the @staticmethod decorator and staticmethod() function

What is Static Methods in Python

A static method is a general utility method that performs a task in isolation. Static methods in Python are similar to those found in Java or C++.

A static method is bound to the class and not the object of the class. Therefore, we can call it using the class name.

A static method doesn’t have access to the class and instance variables because it does not receive an implicit first argument like self and cls. Therefore it cannot modify the state of the object or class.

The class method can be called using ClassName.method_name() as well as by using an object of the class.

Define Static Method in Python

Any method we create in a class will automatically be created as an instance method. We must explicitly tell Python that it is a static method using the @staticmethod decorator or staticmethod() function.

Static methods are defined inside a class, and it is pretty similar to defining a regular function. To declare a static method, use this idiom:

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...Code language: Python (python)

Example: Create Static Method Using @staticmethod Decorator

To make a method a static method, add @staticmethod decorator before the method definition.

The @staticmethod decorator is a built-in function decorator in Python to declare a method as a static method. It is an expression that gets evaluated after our function is defined.

In this example, we will create a static method gather_requirement() that accepts the project name and returns all requirements to complete under this project.

Static methods are a special case of methods. Sometimes, you’ll write code that belongs to a class, but that doesn’t use the object itself at all. It is a utility method and doesn’t need an object (self parameter) to complete its operation. So we declare it as a static method. Also, we can call it from another method of a class.

Output:

Completed task_1
Completed task_2
Completed task_3

Advantages of a Static Method

Here, the static method has the following advantages

  • Consume Less memory: Instance methods are object too, and creating them has a cost. Having a static method avoids that. Let’s assume you have ten employee objects and if you create gather_requirement() as a instance method then Python have to create a ten copies of this method (seperate for each object) which will consume more memeory. On the other hand static method has only one copy per class.
  • To Write Utility functions: Static methods have limited use because they don’t have access to the attributes of an object (instance variables) and class attributes (class variables). However, they can be helpful in utility such as conversion form one type to another. The parameters provided are enough to operate.
  • Readabiltity: Seeing the @staticmethod at the top of the method, we know that the method does not depend on the object’s state or the class state.

The staticmethod() function

Some code might use the old method of defining a static method, using staticmethod() as a function rather than a decorator.

You should only use staticmethod() function to define static method if you have to support older versions of Python (2.2 and 2.3). Otherwise, it is recommended to use the @staticmethod decorator.

Syntax:

staticmethod(function)Code language: Python (python)
  • function: It is the name of the method you want to convert as a static method.
  • It returns the converted static method.

Example:

The staticmethod() approach is helpful when you need a reference to a function from a class body and you want to avoid the automatic transformation to the instance method.

Call Static Method from Another Method

Let’s see how to call a static method from another static method of the same class. Here we will class a static method from a class method.

Output:

static method 1

Referance: Static method documentation

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ