Python staticmethod() Function: Create Static Methods | Syntax, Examples and Use Cases

Introduction: Python staticmethod() Function

A class can contain methods that do not need to access object data or class data. These methods are often used for helper tasks or simple calculations.

Creating a regular instance method for these tasks adds extra work. The Python staticmethod() Function provides a simpler way to define them.

What it is: The staticmethod() function is a built-in Python function that creates a static method. A static method belongs to a class but does not receive self or cls automatically.

See a quick example to understand how the Python staticmethod() Function works.

You can also check its real-world use cases to see where this function is useful.

Now, let’s look at its syntax, parameters, and return value before moving to practical examples and use cases.

💡 Tip: Use the staticmethod() function when a method belongs to a class but does not need to access instance or class data. To explore more built-in functions, visit the Python Built-in Functions Learning Guide.

Syntax, Parameters, Return Value and Examples: Python staticmethod() Function

The section below covers the syntax, parameters, return value, and a quick example of the Python staticmethod() Function.

Syntax

staticmethod(function)

Parameters

Parameter Description
function The function that will be converted into a static method.

Return Value

Return Value Description
staticmethod Returns a static method object.

Quick Example

The following example creates a simple static method.

class Calculator:

    @staticmethod
    def add(a, b):
        return a + b

print(Calculator.add(10, 5))

# Output:
15

The @staticmethod decorator creates a static method. The method can be called directly using the class name without creating an object.

How the Python staticmethod() Function Works

  • The staticmethod() function creates a static method.
  • A static method belongs to the class.
  • It does not receive self automatically.
  • It does not receive cls automatically.
  • It can be called using the class name or an object.
  • It is useful for helper methods that do not need object or class data.

Examples: Python staticmethod() Function

The examples below show different ways to use the Python staticmethod() Function:

Example 1: Creating a Static Method

class Calculator:

    @staticmethod
    def add(a, b):
        return a + b

print(Calculator.add(10, 5))


# Output:
15

Explanation: The @staticmethod decorator creates a static method. Since the method does not use object data, it can be called directly using the class name.

Example 2: Calling a Static Method Using an Object

class Calculator:

    @staticmethod
    def multiply(a, b):
        return a * b

calc = Calculator()

print(calc.multiply(6, 4))


# Output:
24

Explanation: A static method can also be called through an object. The result is the same because the method does not depend on the object.

Example 3: Using a Static Method for Unit Conversion

class Converter:

    @staticmethod
    def celsius_to_fahrenheit(c):
        return (c * 9 / 5) + 32

print(Converter.celsius_to_fahrenheit(25))


# Output:
77.0

Explanation: The conversion only needs the value passed to the method. It does not need any information stored inside the class.

Example 4: Using a Static Method for Validation

class User:

    @staticmethod
    def is_adult(age):
        return age >= 18

print(User.is_adult(20))


# Output:
True

Explanation: Static methods are useful for small helper tasks. Here, the method checks whether the given age is 18 or more.

Example 5: Using Multiple Static Methods

class MathTools:

    @staticmethod
    def square(num):
        return num * num

    @staticmethod
    def cube(num):
        return num * num * num

print(MathTools.square(5))
print(MathTools.cube(5))


# Output:
25
125

Explanation: A class can contain several static methods. Each one performs a separate task without using object or class data.

Example 6: Calling a Static Method from Another Method

class Calculator:

    @staticmethod
    def add(a, b):
        return a + b

    def show_result(self):
        print(Calculator.add(8, 2))

calc = Calculator()

calc.show_result()


# Output:
10

Explanation: The show_result() method calls the static method using the class name. Since add() does not use self or cls, it works like a normal helper function inside the class.

Example 7: Creating a Static Method with staticmethod()

class Greeting:

    def message():
        return "Welcome"

    message = staticmethod(message)

print(Greeting.message())


# Output:
Welcome

Explanation: Instead of using the @staticmethod decorator, you can create a static method by calling the staticmethod() function directly.

Use Cases: When to use the staticmethod() Function

The Python staticmethod() Function is useful in situations like these:

  • Creating helper methods inside a class.
  • Performing calculations that do not use object data.
  • Writing utility methods.
  • Validating input values.
  • Converting one value into another.
  • Grouping related functions inside a class.

Key Takeaways: staticmethod() Function

Here are the main points to remember about the Python staticmethod() Function:

  • The staticmethod() function creates a static method.
  • A static method belongs to the class.
  • It does not receive self automatically.
  • It does not receive cls automatically.
  • It can be called using the class name or an object.
  • It is useful for helper methods that do not use object or class data.

The staticmethod() function is a good choice when a method belongs to a class but does not need object or class information. It helps keep related methods together while making the class easier to organize.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top