Skip to content
Home / Fundamentals

Python staticmethod & classmethod

In Python, there are two built-in decorators that can be used to define methods within a class: @staticmethod and @classmethod. These decorators allow us to define methods that are not bound to the instance of the class (i.e., they do not depend on self) and can be called on the class itself, rather than on a specific instance of the class.

@staticmethod

A static method is a method that belongs to a class rather than an instance of the class. It can be called on the class itself, as well as on any instance of the class. A static method is defined using the @staticmethod decorator, like so:

class MyClass:
    def __init__(self, value):
        self.value = value

    @staticmethod
    def static_method():
        print("I am a static method")

To call a static method, we can use the syntax MyClass.static_method() or instance_of_my_class.static_method(). Both will produce the same output:

MyClass.static_method()  # Output: "I am a static method"

instance = MyClass(5)
instance.static_method()  # Output: "I am a static method"

Static methods are often used to define utility functions that are related to a class, but do not need to access any class-specific state. For example, we might define a is_even static method to check if a given number is even:

class Number:
    def __init__(self, value):
        self.value = value

    @staticmethod
    def is_even(value):
        return value % 2 == 0

We can then call this static method on the class itself, or on any instance of the class:

Number.is_even(4)  # Output: True
Number.is_even(5)  # Output: False

even_number = Number(4)
even_number.is_even(4)  # Output: True
even_number.is_even(5)  # Output: False

@classmethod

A class method is a method that belongs to a class rather than an instance of the class. It can be called on the class itself, as well as on any instance of the class. A class method is defined using the @classmethod decorator, like so:

class MyClass:
    def __init__(self, value):
        self.value = value

    @classmethod
    def class_method(cls):
        print("I am a class method")

To call a class method, we can use the syntax MyClass.class_method() or instance_of_my_class.class_method(). Both will produce the same output:

MyClass.class_method()  # Output: "I am a class method"

instance = MyClass(5)
instance.class_method()  # Output: "I am a class method"

The first argument of a class method is always cls, which refers to the class itself. This allows the class method to access and modify the class state. For example, we might define a set_value class method to set the value of an instance of the Number class:

class Number:
    def __init__(self, value):
        self.value = value

    @classmethod
    def set_value(cls, value):
        cls.value = value

We can then call this class method on the class itself, or on any instance of the class:

Number.set_value(10)

even_number = Number(4)
even_number.set_value(20)

Class methods are often used as alternative constructors for a class. For example, we might define a from_string class method to create an instance of the Number class from a string representation of a number:

class Number:
    def __init__(self, value):
        self.value = value

    @classmethod
    def from_string(cls, string):
        return cls(int(string))

We can then use this class method to create an instance of the Number class:

number = Number.from_string("10")  # Creates an instance of Number with value 10
print(number.value)  # Output: 10

In summary, staticmethod and classmethod are useful decorators that allow us to define methods within a class that are not bound to the instance of the class. Static methods are used to define utility functions that do not need to access any class-specific state, while class methods are used to access and modify the class state and can also be used as alternative constructors for a class.