Daily Python Projects

Daily Python Projects

Create magic methods for Rectangle class (OOP)

Level: Real-World

Ardit Sulce's avatar
Ardit Sulce
May 17, 2024
∙ Paid
2
Share

Project Brief

Yesterday, we coded a Rectangle class that had a width and a height attribute. The class also had an area(), perimeter(), and an is_square() method.

Your task for today is to add two magic methods to that class __str__ and __eq__. The __str__ method should return a string representation of the Rectangle and __eq__ should enable comparing two different rectangle instances returning True or False based on whether the rectangles are similar or not (i.e., have the same width and height).

Expected Output

Keep reading with a 7-day free trial

Subscribe to Daily Python Projects to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Ardit Sulce
Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture