Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement Polymorphic describe() Methods

Define polymorphic describe() methods for different shapes so code can treat them uniformly.

Python practice10 minInheritance & PolymorphismBeginnerLast updated April 8, 2026

Problem statement

You have a Shape base class with a describe() method intended to be overridden by subclasses. Implement two subclasses, Circle and Rectangle, each providing a describe() method returning a string that summarizes the shape and its dimensions. Also implement describe_shape(shape_type, *dims) which constructs the appropriate shape object and returns its describe() string. For unknown shape_type, return the string 'Unknown shape'. - For Circle, the describe string should be: "Circle(radius={radius})" - For Rectangle, the describe string should be: "Rectangle(width={width},height={height})"

Task

Create subclasses that implement a common describe() interface so different shape objects can be described polymorphically.

Examples

Describe a circle

Input

describe_shape("circle", 2)

Output

Circle(radius=2)

Circle.describe() returns the radius formatted into the string.

Input format

A call to describe_shape(shape_type: str, *dimensions). For circle provide one dimension (radius). For rectangle provide two dimensions (width, height).

Output format

A single string describing the shape and its dimensions, or 'Unknown shape' for unsupported types.

Constraints

Do not use any external libraries. Dimensions may be integers or floats, including 0 or negative values for testing. shape_type is case-insensitive (treat 'Circle' and 'circle' the same).

Samples

Sample 1

Input

describe_shape("rectangle", 3, 4)

Output

Rectangle(width=3,height=4)

Rectangle.describe() combines width and height in the expected format.