← Quiz: Magic Methods and Properties: all 10 questions
Quiz: Magic Methods and Properties · Question 5 of 10
What does this print? (code)
What does this print?
class V:
def __init__(self, x):
self.x = x
def __add__(self, o):
return V(self.x + o.x)
print((V(2) + V(5)).x)
- A.25
- B.7
- C.An error
- D.V(7)
Answer & explanation
Correct answer: 7
- calls add, which returns a new V with x = 7.
Can you get the rest right without peeking?
Take the timed Quiz: Magic Methods and Properties quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →