In Python classes, methods differ in their “first argument” and “how they are called.” You need to use them appropriately based on their role.
- Instance Method: Manipulates data for individual objects (instances).
- Class Method: Manipulates data for the entire class (class variables).
- Static Method: Performs independent processing that does not depend on the state of the class or instance.
This article explains how to define and distinguishing these three methods, using a product management class as an example.
1. Instance Method
This is the most common and basic method. It always receives self as the first argument. Use this when you need to access or modify data specific to an instance (instance variables) via self.
Definition and Usage Example
class Product:
# Class variable (Consumption tax rate common to all products)
tax_rate = 0.1
def __init__(self, name, price):
self.name = name
self.price = price
# --- Instance Method ---
def show_detail(self):
"""
Display details of an individual product.
Can access self.name and self.price.
"""
total_price = int(self.price * (1 + Product.tax_rate))
print(f"Product: {self.name} | Base Price: {self.price} yen | With Tax: {total_price} yen")
# Instantiation
item1 = Product("Laptop", 100000)
item2 = Product("Mouse", 2000)
# Call the method from the instance
item1.show_detail()
item2.show_detail()
Output:
Product: Laptop | Base Price: 100000 yen | With Tax: 110000 yen
Product: Mouse | Base Price: 2000 yen | With Tax: 2200 yen
show_detail uses self to display information for specific instances like item1 or item2.
2. Class Method
This method performs processing related to the entire class. You add the @classmethod decorator right before the definition and receive cls (the class itself) as the first argument. It is mainly used to “change class variables” or as an alternative means of instance creation (Factory Method).
Definition and Usage Example
class Product:
tax_rate = 0.1
def __init__(self, name, price):
self.name = name
self.price = price
# --- Class Method ---
@classmethod
def change_tax_rate(cls, new_rate):
"""
Change the consumption tax rate common to all products.
Access class variables using cls.tax_rate.
"""
print(f"Changing tax rate from {cls.tax_rate} to {new_rate}.")
cls.tax_rate = new_rate
# Calculate with current tax rate
print(f"Tax rate before change: {Product.tax_rate}")
# Call the class method to change the tax rate (Usually called from the class name)
Product.change_tax_rate(0.15)
print(f"Tax rate after change: {Product.tax_rate}")
Output:
Tax rate before change: 0.1
Changing tax rate from 0.1 to 0.15.
Tax rate after change: 0.15
change_tax_rate is not interested in the price of a specific product but uses cls to manipulate tax_rate, which is a setting for the entire class.
3. Static Method
This method is defined inside a class but does not depend on either the instance (self) or the class (cls). You add the @staticmethod decorator right before the definition. It takes neither self nor cls as the first argument. Use this when you want to group “utility processing (calculations, checks, etc.)” that does not use class data but is highly relevant to the class.
Definition and Usage Example
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
# --- Static Method ---
@staticmethod
def is_valid_price(price):
"""
Check if the price is valid (0 or more).
Uses neither self nor cls, judges only by the argument 'price'.
"""
return price >= 0
# Using the method
price_check1 = Product.is_valid_price(5000)
price_check2 = Product.is_valid_price(-100)
print(f"Is 5000 yen valid?: {price_check1}")
print(f"Is -100 yen valid?: {price_check2}")
Output:
Is 5000 yen valid?: True
Is -100 yen valid?: False
is_valid_price is simple logic to check numbers and does not require the state of the Product class or specific instance information. However, since it is in the context of “product price checking,” it is defined as a static method because it is more natural for it to belong to this class.
Summary
| Type | Decorator | First Argument | Purpose |
| Instance Method | None | self | Manipulate data per instance |
| Class Method | @classmethod | cls | Manipulate class variables, Factory methods |
| Static Method | @staticmethod | None | Independent processing not relying on class state |
Using these appropriately clarifies the role of the class, resulting in readable and maintainable code.
