Sobes.tech
Junior — Middle

How do you declare a static method in Python?

sobes.tech AI

Answer from AI

In Python, a static method is declared using the @staticmethod decorator. Such a method does not automatically take self or cls as the first argument and behaves like a regular function but is located inside a class.

Example:

class MyClass:
    @staticmethod
    def greet(name):
        return f"Hello, {name}!"

print(MyClass.greet("Alice"))  # Outputs: Hello, Alice!

Static methods are useful when a function is logically related to the class but does not depend on the state of an instance or the class itself.