Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Initialize Multiple Instances with Different Data

Create a simple Rectangle class and a helper that builds many instances from input data.

Python practice15 minClasses & Objects FundamentalsIntermediateLast updated April 7, 2026

Problem statement

Implement a Rectangle class with a constructor that accepts width and height (numbers) and an area() method that returns the rectangle's area (width * height). Also implement a make_rectangles(data) function that takes a list of (width, height) tuples and returns a list of Rectangle instances, one per tuple.

Task

Implement a constructor and an instance method; write a helper function that initializes multiple instances from a list of data tuples.

Examples

Two rectangles

Input

sum(r.area() for r in make_rectangles([(2,3),(4,5)]))

Output

26

Areas are 6 and 20; sum is 26.

Input format

A list of tuples: [(width1, height1), (width2, height2), ...]. Each number can be zero or positive.

Output format

A list of Rectangle instances from make_rectangles; area() returns numeric area. Tests will often sum areas to verify correctness.

Constraints

Widths and heights are integers (can be 0). Do not print inside the classes or helper function.

Samples

Sample 1

Input

sum(r.area() for r in make_rectangles([(7,6)]))

Output

42

Single rectangle area 7*6 = 42.