Course topics

By WebNest Studio

Python Tutorial

Instance, Class and Static Methods

Python classes can contain three kinds of methods, distinguished by what they receive as their first argument. Instance methods receive the object (self), class methods receive the class (cls), and static methods receive nothing special. Choosing the right kind makes a class's design clearer.

This lesson explains each type, when to use it, and how they behave with inheritance.

Instance Methods

The default. They work with a particular object's data through self and can also reach the class through self.__class__. Most methods are instance methods: account.deposit(), order.total().

Class Methods

Decorated with @classmethod, they receive the class as cls. Use them for alternative constructors and for working with class-level state. Because cls is the class the method was called on, a subclass calling an inherited class method gets instances of the subclass — they are inheritance-friendly.

Static Methods

Decorated with @staticmethod, they receive neither the object nor the class. They are ordinary functions placed inside the class because they logically belong there — validation helpers, unit conversions. If a helper does not relate to the class at all, a module-level function is usually better.

Examples

All three method types in one class

Python
class Temperature:
    unit_label = "°C"
    readings = 0

    def __init__(self, celsius):
        self.celsius = celsius
        Temperature.readings += 1

    def to_fahrenheit(self):                    # instance method
        return self.celsius * 9 / 5 + 32

    @classmethod
    def from_fahrenheit(cls, fahrenheit):       # class method
        return cls(round((fahrenheit - 32) * 5 / 9, 1))

    @classmethod
    def total_readings(cls):
        return cls.readings

    @staticmethod
    def is_valid(celsius):                      # static method
        return celsius >= -273.15

t = Temperature(25)
print(t.to_fahrenheit())
print(Temperature.from_fahrenheit(98.6).celsius)
print(Temperature.is_valid(-300), Temperature.is_valid(10))
print(Temperature.total_readings())
Output
77.0
37.0
False True
2

Class methods respect inheritance; static methods do not know the class

Python
class Shape:
    def __init__(self, size):
        self.size = size

    @classmethod
    def unit(cls):
        return cls(1)

    @staticmethod
    def describe():
        return "a shape"

class Square(Shape):
    def area(self):
        return self.size ** 2

s = Square.unit()                  # cls is Square, so we get a Square
print(type(s).__name__, s.area(), Square.describe())
Output
Square 1 a shape

Common Mistakes

  • Forgetting @classmethod or @staticmethod, so Python passes self unexpectedly.
  • Using a static method for a factory, which then always creates the base class instead of subclasses.
  • Putting unrelated utility functions inside classes as static methods.
  • Modifying class state through self (self.count += 1) instead of cls or the class name.

Key Points to Remember

  • Instance methods get self and work with object data.
  • @classmethod methods get cls; ideal for factories and class state.
  • @staticmethod methods get neither; they are namespaced helper functions.
  • Class methods create the correct subclass when inherited.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.