Python Class Method vs Static Method

In this tutorial, We will learn the Python class method vs static method in detail. In a Python webinar, someone asked about the differences between the class method and the static method. I explored this topic and I will share my findings in this article. Let us learn more about this topic today.

Methods in Python

In Python, methods are functions defined within a class that describe the behaviors of an object. While instance methods operate on instances of the class, class methods and static methods serve different purposes.

Read Square Root in Python

Class Method in Python

A class method is a method that is bound to the class and not the instance of the class. It takes cls as its first parameter, which refers to the class itself, rather than an instance of the class. This allows class methods to modify class state that applies across all instances of the class.

To define a class method, you use the @classmethod decorator. Here’s an example:

class MyClass:
    class_variable = "Hello, World!"

    @classmethod
    def class_method(cls):
        return cls.class_variable

In this example, class_method can access class_variable directly through the cls parameter.

Check out Happy Birthday Code in Python

Characteristics of Python Class Methods

  • Bound to the class, not the instance.
  • Can access and modify class state.
  • Defined with the @classmethod decorator.

Static Method in Python

A static method is a method that does not operate on an instance or class. It does not take self or cls as its first parameter. Static methods are defined using the @staticmethod decorator and can be called on the class itself or an instance of the class. They behave like plain functions but belong to the class’s namespace.

Here’s an example:

class MyClass:
    @staticmethod
    def static_method():
        return "This is a static method"

In this example, static_method does not access or modify the class state or instance state.

Read Difference Between Linear Search and Binary Search in Python

Characteristics of Python Static Methods

  • Not bound to the class or instance.
  • Cannot access or modify class or instance state.
  • Defined with the @staticmethod decorator.

Key Difference Between Class Method and Static Method in Python

FeatureClass MethodStatic Method
BindingClass (cls)Neither class nor instance
AccessCan access and modify class stateCannot access or modify class/instance state
Decorator@classmethod@staticmethod
Use CaseFactory methods, modifying class stateUtility functions, independent operations

Check out How to Use the insert() Function in Python?

Use Cases

Let us go through some use cases of class methods and static methods in Python.

Usage of Python Class Methods

Class methods are particularly useful for factory methods that instantiate objects in different ways. They can also be used to modify class variables that affect all instances of the class.

Example:

class Pizza:
    diameter = 12

    def __init__(self, ingredients):
        self.ingredients = ingredients

    @classmethod
    def set_diameter(cls, diameter):
        cls.diameter = diameter

    @classmethod
    def margherita(cls):
        return cls(["mozzarella", "tomatoes"])

# Usage
Pizza.set_diameter(16)
pizza = Pizza.margherita()
print(pizza.ingredients)  
print(Pizza.diameter)

Output:

['mozzarella', 'tomatoes']
16

I have executed the above example code and added the screenshot below.

Python Class Method vs Static Method

Read How to Use the arange() Function in Python?

Usage of Python Static Methods

Static methods are ideal for utility functions that perform a task in isolation. They can be used for operations that do not require access to class or instance data.

Example:

class Math:
    @staticmethod
    def add(x, y):
        return x + y

# Usage
result = Math.add(5, 3)
print(result)

Output:

8

I have executed the above example code and added the screenshot below.

Python Class Method vs Static Method Usage of Static Methods

Check out How to Use the Python Pass Function?

Examples

Let us see some examples of class methods and static methods in Python.

Class Method Example

class Car:
    base_price = 10000

    def __init__(self, model, year):
        self.model = model
        self.year = year

    @classmethod
    def update_base_price(cls, new_price):
        cls.base_price = new_price

    @classmethod
    def from_string(cls, car_string):
        model, year = car_string.split('-')
        return cls(model, int(year))

# Usage
Car.update_base_price(12000)
car1 = Car.from_string("Toyota-2020")
print(car1.model) 
print(car1.year)   
print(Car.base_price)  

Output:

Toyota
2020
12000

Read Difference Between *args and **kwargs in Python

Static Method Example

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

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

# Usage
temp_f = Temperature.celsius_to_fahrenheit(30)
temp_c = Temperature.fahrenheit_to_celsius(86)
print(temp_f)
print(temp_c)  

Output:

 86.0
30.0

Summary

FeatureClass MethodStatic Method
BindingClass (cls)Neither class nor instance
AccessCan access and modify class stateCannot access or modify class/instance state
Decorator@classmethod@staticmethod
Use CaseFactory methods, modifying class stateUtility functions, independent operations

Read How to Use the trim() Function in Python?

Conclusion

In this tutorial, I have explained the difference between the Python class method and static method. We saw about methods in Python, characteristics of class methods and static methods in Python , and key differences between class methods and static methods in Python, use cases and examples.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.