What are static methods in a Python class?

PythonServer Side ProgrammingProgramming

Static methods in Python, in comparison to instance methods, are not bound to an object. In other words, object state cannot be accessed or changed by static methods. Additionally, Python does not automatically give the self or cls parameters to static methods. Therefore, the state of the class cannot be accessed or changed by static methods.

Static methods in Python

In actuality, you define utility methods or group functions that have some logical relationships in a class using static methods.

It is very similar to defining a regular function to define static methods inside of a class.


The @staticmethod decorator is used to define static methods −

class name_of_class: @staticmethod def name_of_static_method(parameters_list): pass print ('static method defined')


The output of the above code is mentioned below −

static method defined

Calling a static method

Without having to create a class instance, a static method can be called directly from the class. Only static variables can be accessed by a static method; instance variables are not accessible.


The following syntax is used for calling a static method −



Following is an example for calling a static method using

Name_of_class.static_method_name() and by using an object of the class −

class Animal: @staticmethod def test(a): print('static method', a) # calling a static method Animal.test(12) # calling using object anm = Animal() anm.test(12)


Following is an output of the above code −

static method 12
static method 12


Calling Static method from another method

We will now examine the process of calling a static method from another static method of the same class. Here, we'll distinguish between a static method and a class method:

class Animal : @staticmethod def first_static_method(): print('first_static_method') @staticmethod def second_static_method() : Animal.first_static_method() @classmethod def class_method(cls) : cls.second_static_method() # calling the class method Animal.class_method()


Following is an output of the above code −


Creating a static method using @staticmethod Decorator

Add the @staticmethod decorator before the method definition to make a method static.Python includes a built-in function decorator called @staticmethod that can be used to declare a method as static. It is an expression that is assessed following the definition of our function.


Static methods are a particular type of method. There are times when you'll write code that is part of a class but doesn't use the actual object at all. It is a utility method and can function without an object (self parameter). Since it is static, we declare it that way. Additionally, we can call it from another class method.

As an illustration, let's develop a static method called “information()” that takes a “kingdom" and returns a list of all the requirements that need to be fulfilled for that kingdom −

class Animal(object): def __init__(self, name, species, kingdom): self.name = name self.species = species self.kingdom = kingdom @staticmethod def information(kingdom): if kingdom == 'Animalia': info = ['lion', 'Pantheria', 'Animalia'] else: info = ['lion'] return info # the instance method def call(self): # calling the static method from the instance method info = self.information(self.kingdom) for i in info: print('Information collected', i) anm = Animal('Lion','Pantheria', 'Animalia') anm.call()


Following is an output of the above code −

Information collected lion
Information collected Pantheria
Information collected Animalia

The staticmethod() function

Some programs may define static methods the old-fashioned way by calling staticmethod() as a function rather than a decorator.

If you need to support previous versions of Python (2.2 and 2.3), then you should only define static methods using the staticmethod() function. The @staticmethod decorator is advised in all other cases.


Following is the syntax of staticmethod() function −



The method you want to change to a static method is called function.It returns the static method that was transformed.


Following is an example of staticmethod() function −

class Animal: def test(a): print('static method', a) # converting to the static method Animal.test = staticmethod(Animal.test) # calling the static method Animal.test(5)


Following is an output of the above code −

static method 5

Note − When you require a reference to a function from a class body but don't want the automatic conversion to an instance method, the staticmethod() approach can be useful.

Updated on 23-Nov-2022 07:37:26