Operator overloading

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

@sayanalu9091
Good work, on the point.
Keep learning more to get expertise.

1 Like

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

@amarjeetkumar1605

Your answer is perfect.
Keep Practicing.

1 Like