Python Practice › OOP Programs › Program 3

Instance method

EasyOOP Programs · 3 of 48

Write a Python class with an instance method that uses the object’s data.

Advertisement
Output
28.27

Approach

  1. An instance method takes self and reads the attributes.

Solution

Python
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.

More oop programs

← All 48 OOP Programs

Leave a Comment