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)
- False False
- True True
- False True
- 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
- wheels is a class attribute of Car, and it is then assigned through one…
- The child class Student defines its own __init__ and does not call the…
- Three classes in a chain each extend fee() using super(). What does the last…
- A class method reads a counter that __init__ increases on the class. What is…
- fahrenheit is declared with @property and is accessed without brackets. What…
- What is the key difference between a method marked @classmethod and one marked…
- B is a child of A. What do isinstance, a direct type comparison and issubclass…
- D inherits from both B and C, and B and C both inherit from A. Which who()…
- Every class in this diamond calls super().go(). What string does D().go() build?
- The Coin class defines both __str__ and __repr__. What is printed for the…
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