
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What are static methods in a Python class?
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.
Example
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')
Output
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.
Syntax
The following syntax is used for calling a static method −
Name_of_class.static_method_name()
Example
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)
Output
Following is an output of the above code −
static method 12 static method 12
Example
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()
Output
Following is an output of the above code −
first_static_method
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.
Example
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()
Output
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.
Syntax
Following is the syntax of staticmethod() function −
staticmethod(function)
where,
The method you want to change to a static method is called function.It returns the static method that was transformed.
Example
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)
Output
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.
- Related Articles
- What are Class/Static methods in Java?\n
- How do I create static class data and static class methods in Python?
- What are Getters/Setters methods for Python Class?
- Static import the Math Class Methods in Java
- How to assign static methods to a class in JavaScript?
- What are the differences between a static and a non-static class in C#?
- What are static members of a Java class?
- What are static members of a C# Class?
- What are the methods available for handling static dropdowns in a page in Selenium with python?
- Are static methods inherited in Java?
- Why static methods of parent class gets hidden in child class in java?
- Declare static variables and methods in an abstract class in Java
- Class or Static Variables in Python?
- What is a static class in Java?
- What is a static class in C#?
