0
Operator overloading
what is the benefit of overloading operators?
2 Answers
+ 3
So you can treat user defined types as if they were built-in types, you can compare with ==, assign with =, +=, -=, increment-decrement with ++, --, or do other operations to the internal values encapsulated in the user defined type, and yet it looks and feels like working with built-in types.
Hth, cmiiw
0
class BankAccount:
def __init__(self, balance):
self.balance = balance
def _add_(self , other):
return BankAccount(self.balance + other.balance)
a=BankAccount(1024)
b=BankAccount(42)
result = a + b
print(result.balance)
This is ans
for the code coach, try it to know more!