Python Methods
Table of Contents
In the last lesson, we learned about Python Class Variables. After learning about variables, let us learn about methods in Python. A method is a function that belongs to a class.
In this tutorial, we will learn about three types of methods: instance methods, class methods, and static methods.
โ๏ธ What Is a Method in Python?
A method is a function that is defined inside a class. It operates on objects of that class.
The most common type is the instance method. It takes self as the first parameter and can access instance variables.
class Dog: def __init__(self, name): self.name = name
def bark(self): print(f"{self.name} says Woof!")
dog = Dog("Buddy")dog.bark()Output
Buddy says Woof!Here, bark is an instance method. It uses self to access the dogโs name.
๐งฉ Instance Methods in Python
Instance methods are the most common type. They work with a specific object.
They take self as the first parameter. And they can access and modify instance variables.
class Counter: def __init__(self): self.count = 0
def increment(self): self.count += 1
def get_count(self): return self.count
c = Counter()c.increment()c.increment()print(c.get_count())Output
2That is an instance method. It works on the specific object c.
๐๏ธ Class Methods in Python
A class method works with the class, not with a specific object.
We define it using the @classmethod decorator. And the first parameter is cls, which refers to the class.
class Dog: species = "Canis familiaris"
@classmethod def get_species(cls): return cls.species
print(Dog.get_species())Output
Canis familiarisClass methods are often used as alternative constructors. They can create objects in different ways.
class Person: def __init__(self, name, age): self.name = name self.age = age
@classmethod def from_birth_year(cls, name, birth_year): age = 2025 - birth_year return cls(name, age)
person = Person.from_birth_year("Alex", 2000)print(person.name)print(person.age)Output
Alex25That is a useful pattern.
๐ง Static Methods in Python
A static method does not take self or cls. It is just a function that belongs to the class.
We define it using the @staticmethod decorator.
class Math: @staticmethod def add(a, b): return a + b
print(Math.add(5, 3))Output
8Static methods are useful when we want a function that is related to the class but does not need the class or object.
๐ก When to Use Each Type of Method in Python
Use instance methods when the method needs to work with a specific object.
Use class methods when the method needs to work with the class itself, like creating objects in different ways.
Use static methods when the method is related to the class but does not need the class or object.
๐งช A Real-Life Example โ Date Class
class Date: def __init__(self, day, month, year): self.day = day self.month = month self.year = year
def display(self): print(f"{self.day}/{self.month}/{self.year}")
@classmethod def from_string(cls, date_string): day, month, year = map(int, date_string.split("-")) return cls(day, month, year)
@staticmethod def is_valid_date(date_string): parts = date_string.split("-") return len(parts) == 3
date1 = Date(15, 1, 2025)date1.display()
date2 = Date.from_string("20-06-2025")date2.display()
print(Date.is_valid_date("20-06-2025"))Output
15/1/202520/6/2025TrueThat shows all three types of methods working together.
๐ Common Mistakes with Methods in Python
โ Mistake 1: Forgetting self in instance methods
class Dog: def bark(): print("Woof!")This gives an error when we call bark().
Fix:
class Dog: def bark(self): print("Woof!")๐ซ Mistake 2: Using self in class methods
class Dog: @classmethod def get_species(self): return "Canis familiaris"This works but is confusing. The first parameter should be cls.
Fix:
@classmethoddef get_species(cls): return "Canis familiaris"๐ Mistake 3: Forgetting the decorator
class Math: def add(a, b): return a + bThis is an instance method, not a static method. It will not work as expected.
Fix:
class Math: @staticmethod def add(a, b): return a + bNext, we will learn about Python Inheritance and reuse and extend behavior from another class.