logo CodeStepByStep logo


Language/Type: Python classes
Related Links:

Add a method named __eq__ to the Rectangle class. Use your Rectangle class solution from the previous exercises as a starting point to solve this exercise.

Suppose a class named Rectangle has already been created that has the following members:

member name type description
Rectangle(x, y, w, h) constructor Initializes a new rectangle whose top-left corner is specified by the given (x, y) coordinates and with the given width and height, w by h. Raises a ValueError on a negative width or height.
data attributes private attributes storing rectangle coordinates
property the rectangle location and dimensions (read-only)
str(r) method returns a string representation of the rectangle, such as "Rectangle[x=1,y=2,width=3,height=4]".

Add a method named __eq__ to your class. Your method should accept another rectangle as a parameter and return True if the two rectangles have exactly the same state, including their x, y, width, and height.

Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.