+ 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?

22nd Mar 2021, 3:17 AM
Mania Sacha
Mania Sacha - avatar
4 Respostas
+ 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)
22nd Mar 2021, 6:02 AM
A͢J
A͢J - avatar
+ 3
Mania Sacha Where is your code?
22nd Mar 2021, 5:00 AM
A͢J
A͢J - avatar
+ 1
Thanks man 😌
22nd Mar 2021, 6:03 AM
Mania Sacha
Mania Sacha - avatar
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)
22nd Mar 2021, 5:56 AM
Mania Sacha
Mania Sacha - avatar