Skip to main content

Object-Oriented Python

Class Methods and Static Methods

0:00
LearnStep 1/3

Method Types in Python

In Python, classes can define three distinct types of methods, each serving a specific architectural purpose. Choosing the right one is crucial for clean, idiomatic code.

1. Instance Methods

These are the default methods. They receive the instance as the first argument, conventionally named self. Use these when you need to read or modify the state of a specific object.

2. Class Methods (@classmethod)

Decorated with @classmethod, these methods receive the class itself as the first argument, conventionally named cls. They cannot access unique instance data (self) but can access class attributes. Their primary use case is factory methods or alternative constructors.

3. Static Methods (@staticmethod)

Decorated with @staticmethod, these methods do not receive an implicit first argument (no self or cls). They behave like regular functions but reside within the class's namespace for organizational purposes. Use them for utility functions that don't need to access the class or instance state.

python