+ 1
Magic method practice (Bank account)
I tried few ways to solve it but none worked. Is there anyone to explain it to me please?
4 Answers
+ 5
Mania Sacha
Don't add result in __init__ method and return self.balance + result.balance in __add__ method
Just print result not result.balance
class BankAccount:
def __init__(self, balance):
self.balance = balance
def __add__(self, result):
return (self.balance + result.balance)
a = BankAccount(1024)
b = BankAccount(42)
result = a + b
print(result)
+ 3
Mania Sacha
Where is your code?
+ 1
Thanks man đ
0
class BankAccount:
def __init__(self, balance, result):
self.balance = balance
self.result = result
#def __add__(self, result):
# cont = self.result + self.balance
# return BankAccount(result)
a = BankAccount(1024)
b = BankAccount(42)
result = a + b
print(result.balance)