Python @staticmethod

Python @staticmethod decorator is used to transform a method into a static method.

In this tutorial, we will learn about the syntax of Python staticmethod() function, and learn how to use this function with the help of examples.

Syntax

The syntax to use @staticmethod decorator to define a function f as static method in class A is

class A:
    @staticmethod
    def f(arg1, arg2, ...): ...

Please note that a static method does not receive an implicit first argument.

Example

In this example, we define a class A, with f as static method.

Python Program

class A:
    @staticmethod
    def f():
        print('Hello World')

A.f()
Try Online

Output

Hello World

A static method does not require a class instance. We can just use the class name to call the static method.

Conclusion

In this Python Tutorial, we have learnt the syntax of Python staticmethod() builtin function, and also learned how to use this function, with the help of Python example programs.