this is a features in python that allow a single operator to use in different function.
like like = is meant to be assign something… and += means “add and”
2 Likes
Suppose if we have a class called Complex that represents complex numbers, we could overload the +
operator to add two Complex objects together. For example
class Complex:
def init(self, real, imag):
self.real = real
self.imag = imag
# add two objects
def __add__(self, other):
return self.real + other.real, self.imag + other.imag
obj1 = Complex(1, 2)
obj2 = Complex(5, 4)
obj3 = obj1 + obj2
print(obj3)
Output-10,4
Amarjeet Kumar
1 Like