MediumPython

OOP — Shape Inheritance

PythonOOPClasses

Implement solve() that returns a tuple of three classes: (Shape, Circle, Rectangle).

Requirements:

Shape (base class)

  • Constructor: __init__(self, color: str)
  • Method: describe(self) -> str returns "I am a {color} shape."

Circle (extends Shape)

  • Constructor: __init__(self, color: str, radius: float)
  • Method: area(self) -> float returns π × radius² (use math.pi)
  • Overrides describe(self)"I am a {color} circle with radius {radius}."

Rectangle (extends Shape)

  • Constructor: __init__(self, color: str, width: float, height: float)
  • Method: area(self) -> float returns width × height
  • Overrides describe(self)"I am a {color} rectangle of {width}x{height}."

Example

Shape, Circle, Rectangle = solve()
c = Circle("red", 5)
c.describe()  # "I am a red circle with radius 5."
c.area()      # 78.539...

Sample tests

Test #1Shape base describe
Input: [[]]
Output: {"shape_describe":"I am a blue shape."}
Test #2Circle describe override
Input: [[]]
Output: {"circle_describe":"I am a red circle with radius 5."}
Test #3Circle area ≈ π×5²
Input: [[]]
Output: {"circle_area_approx":78.53}
Test #4Rectangle describe override
Input: [[]]
Output: {"rectangle_describe":"I am a green rectangle of 4x6."}
Test #5Rectangle area 4×6
Input: [[]]
Output: {"rectangle_area":24}