By WebNest Studio Engineering Team
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.
Why it matters
The three kinds of method differ in what they can reach: the object, the class, or neither. Choosing the right one tells the reader what the method depends on, and class methods in particular are how factory methods are written so that they keep working in subclasses. This is a regular interview question.
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
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())
77.0
37.0
False True
2
Class methods respect inheritance; static methods do not know the class
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())
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.
Exercise
Complete the Pizza class with an instance method describe, a class method margherita that creates a pizza with cheese and tomato, and a static method is_valid_size that accepts only 8, 12 and 16.
Expected output: Pizza with cheese, tomato then True
class Pizza:
def __init__(self, toppings):
self.toppings = toppings
# TODO: instance method describe() -> "Pizza with cheese, tomato"
# TODO: class method margherita() -> a Pizza with ["cheese", "tomato"]
# TODO: static method is_valid_size(size) -> True for 8, 12 or 16
print(Pizza.margherita().describe())
print(Pizza.is_valid_size(12))
Show hints
", ".join(self.toppings)builds the list of toppings as text.- A class method takes
clsas its first parameter; a static method takes neitherselfnorcls.
Show solution
class Pizza:
def __init__(self, toppings):
self.toppings = toppings
def describe(self):
return "Pizza with " + ", ".join(self.toppings)
@classmethod
def margherita(cls):
return cls(["cheese", "tomato"])
@staticmethod
def is_valid_size(size):
return size in (8, 12, 16)
print(Pizza.margherita().describe())
print(Pizza.is_valid_size(12))
Quick quiz
1. What is passed as the first argument of a class method?
- A. The object
- B. The module
- C. Nothing
- D. The class
Show answer
D. By convention the parameter is named cls.
2. What does a static method receive automatically?
- A. The object
- B. The class
- C. Nothing; only the arguments given by the caller
- D. Both the object and the class
Show answer
C. It is an ordinary function placed inside the class for organisation.
3. Which kind of method can read and change the attributes of one particular object?
- A. Instance method
- B. Class method
- C. Static method
- D. None of them
Show answer
A. Only an instance method receives the object, as self.
Interview questions
What is the difference between @classmethod and @staticmethod?
Show answer
A class method receives the class as its first argument and can read or change class-level state and create instances; called on a subclass, it receives that subclass, which is why it is used for factory methods. A static method receives nothing automatically and cannot reach the class or an object; it is a plain function that lives in the class because it is logically related to it.
When would you use a static method?
Show answer
For a helper that belongs with the class conceptually but needs no data from the object or the class, such as validating a value or converting a unit. If the function needs the class, for example to create an instance, it should be a class method; if it is unrelated to the class, it should be a module-level function.
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 in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering