HomeLinuxPython Name Static Technique Inside Class

Python Name Static Technique Inside Class


In Python, we have now several types of features/strategies, equivalent to built-in features, user-defined features, and plenty of extra. Principally, it’s a block of code that’s solely executed when it’s invoked. Customers can move information into it, generally known as arguments/parameters. Features may be static which is a part of the category and used for making the category’s utility strategies. To name the static strategies, the category reference can be utilized.

The outcomes from this weblog are:

What’s a “static” Technique in Python?

In Python, static strategies may be invoked with out an object for that specific class and can’t replace the place of an object.

Benefits of the Static Strategies in Python

The benefits of the static strategies are listed beneath:

    • Static strategies have clear use instances.
    • Customers could make static strategies once they want performance not with respect to the thing however all the class.
    • In static strategies, customers don’t require to move “self” as the primary parameter.

The way to Invoke “static” Technique inside Class in Python?

To invoke a static technique inside a category in Python, use a static technique from throughout the physique of the outlined class and outline it by using Python’s built-in “@staticmethod” as a decorator.

Instance

At first, we constructed a category. Then, make the most of the “@staticmethod” decorator to outline a static technique. After that, name the category technique through the use of the “ClassnName.method_name” and an object of the category. In our case, our class title is “Linuxhint”, the tactic title is “stat_func()” and the “lxht” is the category object:

class Linuxhint(object):

    @staticmethod # use as decorator
    def stat_func():
    print(“This can be a Static Technique”)    

Linuxhint.stat_func()

lxht = Linuxhint()
lxht.stat_func()

 
Within the below-provided output, we are able to see the generated outcome:


That’s it! We’ve offered a strategy to name the static technique throughout the class in Python.

Conclusion

In Python, static strategies may be invoked with out an object. The “@staticmethod” decorator can be utilized for outlining a static technique. This text illustrated the process for calling a static technique inside a category in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments