Well done!
You have completed Customizing Python with Magic Methods and Operator Overloading Quiz!
Quiz Question 1 of 5
How would you modify the __add__()
method to ensure that adding two Account
objects not only sums their amount
attributes but also concatenates their name
attributes with an underscore between them?
class Account:
def __init__(self, name="", amount=0):
self.name = name
self.amount = amount
Choose the correct answer below:
-
A
def __add__(self, other): return Account(self.amount + other.amount, self.name + "_" + other.name)
-
B
def __add__(self, other): return Account(self.amount, self.name + other.name)
-
C
def __add__(self, other): return Account(self.name + other.name, self.amount + other.amount)
-
D
def __add__(self, other): return Account(self.name + "_" + other.name, self.amount + other.amount)