Section 1/4Defining an Instance Method
Defining an Instance Method
Explore this concept
Methods
A method is a function that belongs to a class. Methods can read and modify the object's own data using self, and they define the behaviour of an object.
Defining an Instance Method
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius ** 2
c = Circle(5)
print(c.area()) # → 78.53975→
78.53975- Methods are defined inside the class body, indented one level
selfis always the first parameter — Python passes the object automatically- Call a method with
object.method()— you do not passselfyourself