Python Practice › OOP Programs › Program 3
Instance method
Write a Python class with an instance method that uses the object’s data.
Output
28.27
Approach
- An instance method takes
selfand reads the attributes.
Solution
class Circle: def __init__(self, radius): self.radius = radius def area(self): return round(3.14159 * self.radius ** 2, 2) c = Circle(3)print(c.area())Output
28.27
Line by line
Lines 5-6
self.radius is the radius of the circle the method was called on.
Learn the concept
New to this topic? Read the lesson, then come back.