Friday, November 15, 2024
Google search engine
HomeLanguagesPython staticmethod() Function

Python staticmethod() Function

Python staticmethod() function is used to convert a function to a static function. A static method is a method that belongs to a class rather than an instance of a class. The static methods do not require instantiation. 

Python Staticmethod() Syntax

Syntax: staticmethod(function)

Parameter: The static method () method takes a single argument i.e. it takes a function as an argument.

Returns: The static method () returns a static method of the function passed as the argument.

What are Static Methods in a Python

In object-oriented programming, a static method is a special type of method that is associated with the class itself instead of any instance or object of the class. It can be directly called on the class without creating an instance of it. Python uses a decorator denoted by ‘@staticmethod’ to identify static methods.

When do you use static methods?

Static methods are used in specific situations where the functionality of a method is related to a class but doesn’t require access to instance-specific data. In certain situations, static methods are utilized when a method’s purpose is linked to a class but does not need access to data specific to an instance. They are beneficial for defining utility functions or operations that are closely tied to a class but do not rely on the state of individual objects.

Python Staticmethod() Example

The staticmethod() function in Python enables the definition of static methods within a class, allowing the creation of methods that do not require access to instance-specific data. Here are examples of using staticmethod() function.

Python3




class MathUtils:
    @staticmethod
    def add(a, b):
        return a + b
 
result = MathUtils.add(9, 8)
print(result)


Output

17

Static method() Without Calling Instance

In the code a class with method greet() is created, then it was converted to the static method using staticmethod(), and it is called without creating an instance, as we have discussed earlier it is not required to create an instance of the class to call a static method.

Python3




class demoClass:
 
    def greet(msg):
        return msg
 
 
# convert the add to a static method
demoClass.greet = staticmethod(demoClass.greet)
 
# we can access the method without
# creating the instance of class
print(demoClass.greet("Demo class is Today"))


Output

Demo class is Today

Static Method() for Mathematical Operations

If we don’t require to use the class properties then we can use static methods, here in the above code add() method does not use any class properties so it is made static using staticmethod() and the diff method needs to be called by creating an instance because it uses class properties.

Python3




class demoClass:
 
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
    def add(a, b):
        return a+b
 
    def diff(self):
        return self.a-self.b
 
 
# convert the add to a static method
demoClass.add = staticmethod(demoClass.add)
 
# we can access the method without creating
# the instance of class
print(demoClass.add(1, 2))
 
# if we want to use properties of a class
# then we need to create a object
Object = demoClass(1, 2)
print(Object.diff())


Output

3
-1

Static method() To Access Class variables

Python static method allows you to directly access class variables. Static methods cannot access instance-specific data since they are not tied to any specific instance of the class. They do, however, have access to class-level information, which includes class variables.

Python3




class MathUtils:
    num = 40
 
    @staticmethod
    def double():
        return MathUtils.num * 2
 
result = MathUtils.double()
print(result)


Output

80

Static method() for String Formatting

String formatting operations can be carried out using static methods. Without having access to instance-specific data, they offer a means of encapsulating string formatting logic within a class.

Python3




class Formatter:
    @staticmethod
    def format_name(first_name, last_name):
        return f"{last_name}, {first_name}"
 
formatted_name = Formatter.format_name("For Geeks", "Geeks")
print(formatted_name)


Output

Geeks, For Geeks

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments