Python

Python Call Static Method Within Class

The static function in Python is part of the Class. They are used to make the class’s utility methods. The Class reference is used to call the static method. Because they are members of the class, they are unable to affect the object’s state. A static method has no accessibility to the attribute values. The static method works similarly to a function in a Python script, but it is located within the class body. A static method can be called from either a class or object reference. We can call it Utils if foo() is a static function in Class Utils. Utils.foo() as well as Utils().foo() . The static methods allow the utility methods to be separated into sub-modules. A static method is paired with a class and not with a particular instance of the class. As a result, the static method is unable to retrieve instance variables. Static methods are defined by using the @staticmethod decorator well before the static method definition.

The @classmethod decorator is indeed a built-in method decorator that is evaluated once your function has been declared. The outcome of that assessment casts doubts over your function definition. In the same way, an instance method receives the instance as an implicit first parameter; a class method gets the class as an implicit first argument. An implicit initial parameter is not passed to a static method. Static methods are connected to the class. The class state cannot be accessed or modified by a static method. It’s in a class, as it clearly explains why the method should be in a class. Now let’s look at some examples.

Example 1:

A static method is a broad utility method that accomplishes a single task. Python’s static measures are related to those in Java and C++. A static method is directly connected to the class and not with the object of the class. As a result, we can refer to it by its class name. A static method has no access to the class as well as, for instance, variables since it does not get a starting argument such as cls and self. It is not possible to change the object’s or class’s state as an outcome. ClassName.method_name() and an object of the class can be used to call the class method.

class Student:
    @staticmethod
    def sample(a):
print('Inside static method', a)
Student.sample(5)
std = Student()
std.sample(5)

Here you can see the generated result.

Example 2:

Some programs may establish a static method by invoking staticmethod() as a function but instead as a decorator. If you need to support previous versions of Python, you should only use the staticmethod() function to define static methods. If you don’t have to use the @staticmethod decorator, be using the @staticmethod decorator instead. The staticmethod() is useful in situations where you look for a connection to a function from a class body but not in favor of the automated transition to the instance method. In the given an example, you can see how to call a static method from some other static method in the same class. We’ll distinguish a static method from a class method in this section.

class Event :
    @staticmethod
    def static_method_One():
print('static method 1')
    @staticmethod
    def static_method_Two() :
Event.static_method_One()
    @classmethod
    def class_method_One(cls) :
cls.static_method_Two()
Event.class_method_One()

Here is how you can call a static method.

Example 3:

We’ll define a class method and then a static method in this example. The purpose of a @staticmethod decorator is to define a static method, and the @classmethod decorator is to define a class method. See the example. Let’s imagine we want to make a Person class. Because Python does not enable method overloadings like C++ or Java, we must define factory methods using class methods. In the example below, we utilize a class function to build a person object from a birth year. To establish whether or not a person is an adult, we use a static method in the example below.

from datetime import date
class Person:
    def __init__(self, your_name, your_age):
        self.your_name = your_name
        self.your_age = your_age
    @classmethod
    def fromBirthYear(cls, your_name, your_year):
        return cls(your_name, date.today().year - your_year)
    @staticmethod
    def isAdult(your_age):
        return your_age> 18
First_p = Person('Alex', 23)
Second_p = Person.fromBirthYear('Alex', 1989)
print (First_p.your_age)
print (Second_p.your_age)
print (Person.isAdult(22))

See the result below.

Difference Between Class method and Static Method

A static process does not involve cls as the initial parameter, but a class method does. A class method can also access the class state and change it, but a static method does not. Static methods usually have no idea of the state of the class. They are utility methods that take parameters and perform operations on them. Class methods should consider class as a parameter. In Python, the @staticmethod decorator is frequently used to develop a static method, whereas the @classmethod decorator is widely used to construct a class method.

Conclusion:

This tutorial covered the basics of class methods, static methods, and ordinary instance methods. A class instance is not required for class methods. They can’t get to the instance, but they can get to the class (cls). Cls and self are not accessible to static methods. Also, they operate in the same way as regular functions, but they’re in the namespace of the class. Static and class methods exchange information and (to some extent) enforce developer intent on class design. This even has the ability to be advantageous in terms of upkeep. You’ll be able to create object-oriented Python that conveys its goal more clearly and is easier to maintain in the long term if you gain an intuitive knowledge of their distinctions.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content