Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement a Computed Property for a Derived Value

Create a Rectangle class with width and height and a read-only computed area property.

Python practice10 minMethods & PropertiesBeginnerLast updated April 7, 2026

Problem statement

Implement a Rectangle class that stores width and height and exposes a read-only area property computed as width * height. Requirements: - Rectangle(width, height) constructor stores numeric width and height. - width and height should be settable attributes (public) that accept numeric values. - Implement an @property called area that returns the current product of width and height. - The area must always reflect the current width and height values (i.e., it is computed on access, not cached).

Task

Learn to implement a computed @property that derives a value from instance state and always reflects updates.

Examples

Compute area and update dimensions

Input

r = Rectangle(3, 4) r.area

Output

12

Area is computed as 3 * 4 = 12. If width or height change, area will reflect the new values.

Input format

A Python expression creating or inspecting a Rectangle instance, evaluated and its return value compared to the expected output.

Output format

The string representation of the returned value is compared to the expected output.

Constraints

- width and height are numeric (int or float). - area should return int or float depending on inputs. - No external libraries. - Use @property for area and compute it on access.

Samples

Sample 1

Input

Rectangle(2.5, 3).area

Output

7.5

Area computed with float inputs yields a float result.