Multiple ways to Declare a static in Python

Static methods are methods, called on a class without using an instance or an object.

This tutorial explains how to declare a static method in a class.

Static methods can be called using the below syntax.

classname.static_method()

Staticmethod decorator to define a static function

Declare a method with decorator @staticmethod and work as a static method.

class Employee(object):
  @staticmethod
  def get_department(self):
    print(self)

Employee.get_department("sales")  # sales

Use staticmethod function to pass a function to make a static method

define a normal function in a class.

Next, the staticmethod() function takes a class function as a parameter, resulting in a static function, assigned to the class function

class Employee:
  def get_department(self):
    print(self)
Employee.get_department=staticmethod(
  Employee.get_department)
Employee.get_department("sales")  # sales

Use @classmethod decorator to define class-level functions

Define a function with @classmethod decorator. A function is declared with an additional argument to define class-level state.

class Employee(object):
  @classmethod
  def get_department(cls,value):
    print(value)

Employee.get_department("sales")  # sales

Use classmethod() function with function name as an argument

Define a normal function in a class.

Next, the classmethod() function takes the class function as a parameter, results in a static function, and is assigned to the class function.

Function needs an additional argument similar to self for instance of a class.

Here is an example

class Employee:
  def get_department(cls,self):
    print(self)
Employee.get_department=classmethod(
  Employee.get_department)
Employee.get_department("sales")  # sales

Declare normal function outside for static

A function declared outside an all-class method, inside a class.

Here is an example

class Employee:
  def get_department(self):
    print(self)

Employee.get_department("sales")  # sales