Question 7
Back to Exercise
What is the output of the following Python program?
class V:
def __init__(self, x, y): self.x, self.y = x, y
def __add__(self, o): return V(self.x + o.x, self.y + o.y)
def __mul__(self, k): return V(self.x * k, self.y * k)
def __eq__(self, o): return (self.x, self.y) == (o.x, o.y)
def __repr__(self): return f"V({self.x}, {self.y})"
a, b = V(1, 2), V(3, 4)
print(a + b, a * 3, a + b == V(4, 6), a == V(1, 2))
- A V(4, 6) V(3, 6) True True Correct
- B V(4, 6) V(1, 2, 1, 2, 1, 2) True True
- C V(4, 6) V(3, 6) False False
- D TypeError
Correct Answer: Option A
+, * and == call __add__, __mul__ and __eq__; print uses __repr__ because there is no __str__.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!