Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create a Read-Only Computed Property

Implement a computed property that provides a read-only view derived from internal state.

Python practice15 minEncapsulation & Access PatternsIntermediateLast updated April 8, 2026

Problem statement

Implement a Rectangle class that stores width and height. Provide a read-only property area that returns width * height. Width and height should be validated in the constructor to be numeric (int or float, but not bool) and non-negative. The area property must be implemented using @property and should compute the result on access (no setter).

Task

Provide a computed read-only property using @property that computes a value from private attributes without allowing assignment.

Examples

Area of a 3x4 rectangle

Input

Rectangle(3, 4).area

Output

12

area computes width * height = 3 * 4 = 12

Input format

A class Rectangle(width, height). Tests will construct rectangles and access the .area property in single-expression form.

Output format

Accessing .area returns the numeric area (int or float) as a value.

Constraints

width and height must be int or float (not bool) and >= 0. area has no setter and should compute width * height.

Samples

Sample 1

Input

Rectangle(2.5, 4).area

Output

10.0

Floating point width is supported; area is 2.5 * 4 = 10.0