Classes & Objects · Question 11 · Medium

The Point class defines __eq__. What do == and is give for two separately created points with the same coordinates?

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __eq__(self, other):
        return (self.x, self.y) == (other.x, other.y)

p = Point(1, 2)
q = Point(1, 2)
print(p == q, p is q)
  1. False False
  2. True True
  3. False True
  4. True False
Answer and explanation

Answer: D. True False

Explanation: == calls __eq__, which compares the coordinates and finds them equal. is checks whether both names point to the very same object, and p and q are two different objects.

Previous: Three classes in a chain each extend fee() using super(). What does the last… · Next: A class method reads a counter that __init__ increases on the class. What is…

Practise classes & objects

Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.

More classes & objects questions

More python practice

Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge