Course Progress55%
🍎 Python OOP Topic 55 / 100
⏳ 8 min read

Instance Methods

Functions that belong to a class and have access to the object’s own data through self.

"Methods are the actions an object can perform. Data describes what an object is. Methods define what it can do."

— ShurAI

What is an Instance Method?

An instance method is a function defined inside a class. It always takes self as its first parameter, which gives it access to the object’s own data. Methods can read attributes, change attributes, and return values:

python
class Counter:
    def __init__(self):
        self.count = 0

    def increment(self):      # modifies the object's data
        self.count += 1

    def reset(self):           # modifies the object's data
        self.count = 0

    def value(self):            # reads and returns data
        return self.count

c = Counter()
c.increment()
c.increment()
c.increment()
print(c.value())    # 3
c.reset()
print(c.value())    # 0

Methods with Parameters

Methods can accept extra arguments beyond self:

python
class Rectangle:
    def __init__(self, width, height):
        self.width  = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * (self.width + self.height)

    def scale(self, factor):        # extra parameter beyond self
        self.width  *= factor
        self.height *= factor

r = Rectangle(4, 6)
print(r.area())        # 24
print(r.perimeter())   # 20
r.scale(2)
print(r.area())        # 96  (8x12)

Methods Calling Other Methods

Use self.method_name() to call one method from another inside the same class:

python
class Circle:
    import math
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        import math
        return math.pi * self.radius ** 2

    def describe(self):
        # calls self.area() from inside another method
        print(f"Radius: {self.radius}, Area: {self.area():.2f}")

c = Circle(7)
c.describe()   # Radius: 7, Area: 153.94

Real Example — Student Grade Tracker

python
class Student:
    def __init__(self, name):
        self.name   = name
        self.scores = []

    def add_score(self, score):
        self.scores.append(score)

    def average(self):
        if not self.scores:
            return 0
        return sum(self.scores) / len(self.scores)

    def grade(self):
        avg = self.average()   # calls another method
        if   avg >= 90: return "A"
        elif avg >= 75: return "B"
        elif avg >= 60: return "C"
        else:           return "F"

    def report(self):
        print(f"{self.name}: avg={self.average():.1f}, grade={self.grade()}")

s = Student("Riya")
for score in [88, 92, 79, 95]:
    s.add_score(score)
s.report()   # Riya: avg=88.5, grade=B

"Good methods do one thing well. A method named report() should report, not calculate. Let other methods calculate, then call them. That is how clean class design works."

— ShurAI

🧠 Quiz — Q1

What makes a method an "instance method"?

🧠 Quiz — Q2

How do you call a method bark() on an object dog1?

🧠 Quiz — Q3

How does one method call another method on the same object?

🧠 Quiz — Q4

A method total(self) does return self.price * self.qty. What does self.price refer to?