0Pricing
Python Academy · Lesson

Instance Methods and self

Write methods that operate on the object's own data.

Introduction

Instance methods operate on the data of a specific object, using self to access and modify its attributes.

Defining Instance Methods

def method(self): inside a class is an instance method. It receives the object as the first argument (self).
class Circle:
    def __init__(self, r): self.r = r
    def area(self):
        import math
        return math.pi * self.r**2
print(Circle(5).area())

All lessons in this course

  1. Classes and the __init__ Method
  2. Instance vs Class Attributes
  3. Instance Methods and self
  4. Magic Methods: __str__ and __repr__
← Back to Python Academy