Python staticmethod() Function

Python3 内置函数Python3 Built-in Functions


staticmethod()It is a built-in function in Python used to convert a method into a static method.

A static method belongs to the class rather than instances; it does not need to access instance attributes or call instance methods.staticmethod()It can be used as a decorator, and can also be called directly.

Word Definition: staticmethodYesstatic methodAbbreviation for (static method).


Basic Syntax and Parameters

Syntax Format

staticmethod(function)

Parameter Description

  • Parameter function:
    • Type: Function object
    • Description: The function to be converted into a static method.

Function Description

  • Return value: Returns a static method object.
  • Characteristics: Static methods do not requireselfthe self parameter, and can be called directly through the class name.

Example

Example 1: Basic Usage

Example

class MathUtils:
    # Use decorator to define a static method (recommended)
    @staticmethod
    def add(a, b):
        return a + b

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

# Call directly through the class name (no need to create an instance)
result = MathUtils.add(3, 5)
print(result)  # Output: 8

result = MathUtils.multiply(4, 6)
print(result)  # Output: 24

Expected output:

8
24

Code analysis:

  1. Static methods use@staticmethodthe decorator to define.
  2. Can be called like an ordinary function, but belongs to the class namespace.
  3. It can be called without creating an instance of the class.

Example 2: Not Using a Decorator

Example

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

    @staticmethod
    def fahrenheit_to_celsius(f):
        return (f - 32) * 5/9

# Call static method
print(Temperature.celsius_to_fahrenheit(0))    # Output: 32.0
print(Temperature.celsius_to_fahrenheit(100)) # Output: 212.0
print(Temperature.fahrenheit_to_celsius(32)) # Output: 0.0

# It can also be called through an instance
temp = Temperature()
print(temp.celsius_to_fahrenheit(25))  # Output: 77.0

Expected output:

32.0
212.0
0.0
77.0

Code analysis:

  • Static methods are often used in utility classes to encapsulate functions unrelated to instances.
  • Static methods can be called via the class name or an instance name.

Example 3: Application in Factory Pattern

Example

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @staticmethod
    def from_dict(data):
        # Create a Person object from a dictionary
        return Person(data['name'], data['age'])

    @staticmethod
    def create_adult(name):
        # Create an adult
        return Person(name, 18)

# Use a static method to create the object
p1 = Person.from_dict({'name': 'Tom', 'age': 20})
print(p1.name, p1.age)  # Output: Tom 20

p2 = Person.create_adult('Jack')
print(p2.name, p2.age)  # Output: Jack 18

Expected output:

Tom 20
Jack 18

Static methods are often used as factory methods to create object instances from different data sources.


Python3 内置函数Python3 Built-in Functions


Other Extensions