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
- Classes and the __init__ Method
- Instance vs Class Attributes
- Instance Methods and self
- Magic Methods: __str__ and __repr__