Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Access Instance Attributes Using self

Practice using self to refer to instance attributes inside methods.

Python practice7 minClasses & Objects FundamentalsBeginnerLast updated April 7, 2026

Problem statement

Define a class named Rectangle. The constructor takes two numeric parameters: width and height, and must store them as instance attributes self.width and self.height. Implement an instance method area(self) that returns the product of the instance's width and height (self.width * self.height).

Task

Implement a Rectangle class with width and height instance attributes and an area() method that uses self to compute area.

Examples

Compute area

Input

Rectangle(3, 4).area()

Output

12

The area is 3 * 4 = 12 using the instance attributes via self.

Input format

Construct a Rectangle with Rectangle(width, height) and call .area().

Output format

Return the numeric area value from the area() method.

Constraints

width and height will be numbers (integers or floats).

Samples

Sample 1

Input

Rectangle(2.5, 4).area()

Output

10.0

Floating point multiplication should produce 10.0.